How to persist a NSMutableArray

冷暖自知 提交于 2019-12-06 02:05:11
Enrico Susatyo

1) If you just want to save an NSMutableArray, then yes, you can use NSUserDefaults.

2) If you don't want to deal with Core Data and set up a proper data structure, then it seems like that's the only option.

3) When you get an NSMutableArray from an NSArray *array, just use [array mutableCopy].

Prince Agrawal

You can better go for CoreData. Its really easy to implement & great to use.

Here is the best tutorial to get started off

Here is the Core Data programming guide from apple

And here is why you should go for Core-Data

Despite of all other opinions, I'll suggest you to use core data only. It seemed too easy with visual interface provided in XCode & tons of great development support.

I'd go against using NSUserDefaults, as To-do list should be expanding in your app, & you might want to perform several operations on those objects. Everytime fetching data from userDefaults don't seem good approach to me.

Why Use NSUserDefaults:

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.

Here is Class Reference

geowar

The short answer is to use the NSArray's writeToURL:atomically: and arrayWithContentsOfURL: methods to write your array to and read your array from a file on disk. A longer answer would justify where that might be… For now I'll skip to the answer: "to a file in a (unique to your app) folder in the '~/Library/Application Support/' folder. (Note: I'm an OSX programmer… there may be a better place for iOS apps… ;-) Here's code:

NSArray * array;    // <<== this is your array (I assume you've created it somewhere…)

// get the document directory URL
NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentsDirectory inDomains:NSUserDomainMask] firstObject];


// append our file name
NSURL *url = [documentDirectoryURL URLByAppendingPathComponent:@"ToDo.data" isDirectory:NO];

#if WRITING_ARRAY
    // write our array to the file
    if (![array writeToURL:url atomically:YES]) {
        NSLog(@"Failed to writeToURL:'%@'", url);
    }
#else // READING_ARRAY
    // read (mutable) array from file
    array = [[NSArray arrayWithContentsOfURL:url] mutableCopy];
#endif

You'd better use Core Data, or some other persistence way like FMDB. Use NSUserDefaults is okay but NSUserDefaults has really poor performance. You can do the demo using NSUserDefaults, but for the final app, a better way is strongly recommended.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!