0

I ve used NSUserDefaults before , to store some variables for the settings section of my application.

However now i am making a recipes application , where the user can type in his own recipes and saves them. Each recipe has a title a description a date etc.. So i guess i ll need an array to save every recipe. But how will i add every new recipe dynamically?

I mean i ll just start to save the recipe in the next position on the array? and what happens if the user deletes one recipe and theres a free position in the array?

My logic here is correct on saving the data? What would you do?

  • 4
    Don't use NSUserDefaults to store user data; that's not what it's for. Look at core data or even property list serialisation. – jrturton Mar 17 '13 at 17:25

1 Answers1

5

NSUserDefaults is not what you're looking for. Technically it would work for what you want, but you're probably better off just making a custom class that has a property for all of the recipe characteristics and making the class conform to the <NSCoding> protocol so that you can convert it to data and write it to a file.

I know that might sound complicated if you've never done it before but it's really not too bad.

Here's an example for implementing <NSCoding>. Ignore the end part where it shows you saving the data to NSUserDefaults.

To save your data, instead of using NSUserDefaults, take a look at this question. It might seem like a lot of code for a small task, but the concept is pretty simple.

Edit:

To convert your object to data, assuming you've already implemented <NSCoding> in your custom class:

YourClass* someObject;

// do whatever you do to fill the object with data

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:someObject];

/*
 Now we create the path to the documents directory for your app
 */

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

/*
 Here we append a unique filename for this object, in this case, 'Some_Recipe'
 */

NSString* filePath = [documentsDirectory stringByAppendingString:@"Some_Recipe"];

/*
 Finally, let's write the data to our file
 */

[data writeToFile:filePath atomically:YES];

/*
 We're done!
 */
Community
  • 1
  • 1
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • thank you very much for your answer. I created a class as suggested in the tutorial for my recipe object , with all the necessary properties of the class. However i am a bit confused on how i can save the data. I need to create a new object of the recipe class , write my data on it , as given from the user but then how exactly do i save it? –  Mar 17 '13 at 18:27
  • i mean the answer says smth about getting the documents directory and then make a file name to write the data using this directory. But what is this directory for me? I have an object "recipe" with data , how do i use that to save it in a file ? –  Mar 17 '13 at 18:39
  • I'll edit my answer with some information more specific to your situation. – eric.mitchell Mar 17 '13 at 19:19
  • that would be incredible Rickay. Thank you for your time. –  Mar 17 '13 at 19:22
  • I just implemented it , looks great! Just a few more things. Now if i want to show the data to the user , how do i get them back from the storage? Moreover , how exactly is the data saved ? Like a big array of arrays(recipes) ? And if i want to delete one recipe , how do i do that? –  Mar 17 '13 at 20:28
  • And when someones stores one recipe and then another etc etc. When he deletes the 2nd recipe he created, the memory will be automatically deallocated? Do i need to specify the next time where the new recipe will be saved? Or all these things are done automatically? –  Mar 17 '13 at 20:31
  • And something last , you say somewhere that we append a unique file name for this object. Do i have to change this if i have many recipes? Or this file name is an array of recipes? –  Mar 17 '13 at 20:51
  • You will need a different filename for each recipe, or you could have an array of recipes stored in one file. For the other questions, I think you'll need to work out those details yourself. Good luck! – eric.mitchell Mar 17 '13 at 21:43
  • But i guess thats what i would like to do right? Have an array of recipes inside the filename. That was the purpose of the question too. How to dynamically save these things. So i guess i musnt change the name every time for every recipe , cause i dont know how man recipes the user wants to create. Or how many of them will delete later and create new ones. –  Mar 17 '13 at 22:02