سؤال

I'm absolutely new to iOS Dev. I started out with the really helpful tutorial on Apple dev website

So i made the simple To-Do List application, as per instructions.

The App: It's a simple to-do list. It has a view, with a single text-field, which takes in an input, which then appends that input to the table-list view. the table list view is my "TO-Do List" which is generated from a NSMutableArray.

The Problem: Whenever i quit and relaunch the application, my List disappears.

The Question: I need a way to persist the NSMutableArray *ToDoList which stores my list.

So is it possible to persist the list? or will i have to make use of some kind of datastore? I did a little research before asking and read something about storing the list in 'NSUserDefaults' - but the problem being, NsUserDefaults, only returns an im-mutable Object

Conclusion:

  1. Can we persist the NsMutable List? If yes how?

  2. Is NSUserDefaults necessary to use? Why?

  3. If (2) is yes, then how do i make the returned immutable object of NsUserDefaults mutable?

Yet again i should tell you, that the only hands on iOS dev i have is from the tutorial. So i would love it if you break it down to the simplest form.

Thank you.

هل كانت مفيدة؟

المحلول

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].

نصائح أخرى

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top