Hey all,
So I need to save an NSArray to a file but the problem is the NSArray has custom objects. Someone else has posted a question like it but heres my problem. Im using the Three20 API and am trying to save the TTLauncher view (It looks like the iphone home view) and the way you do that is as follows:
The method writeToFile: only works if the contents are "property list objects," that is, a combination of NSString, NSData, NSArray, or NSDictionary. It won't work with your custom objects.
If you make your objects conform to NSCoding, however, then you can save an array of them using NSKeyedArchiver instead:
Hey Smasher,
Thanks for the reply. I do have a quick question. In my code I am just calling
[[TTLauncherItem alloc] init]
In yours you had defined all of your variable. How would i do it in my case?
Thanks
-Korki
I assume you mean inside initWithCoder? [[TTLauncherItem alloc] initWithCoder: theCoder] will get called by the system when you load the array from the file. You need to make sure the initWithCoder method you write will init the object and set the variables based on what's in the file.
Inside my initWithCoder method I call [self init] to call the regular init method first, and then I set all of the variables based on the info I get from the coder. In your case you have a different init method you want to call, so you would get the info from the coder and then call that init method:
Hey Smasher,
Thanks for the help and I apologize but I'm fairly new with this. What do I put in the initWithCoder function and the encodeWithCoder function and then in the main function where i call
You should add those methods to the TTLauncherItem class; you can use a category to do that. Then you will not call initWithTitle, except inside the initWithCoder method you wrote.
In your main method you will load the entire array by doing this:
A different method: I did not know what TTLauncherItem was when you asked - I assumed it was a class that you wrote. If you don't want to go with the NSCoding / category method you could create a dictionary with just the data you need, and save an array of dictionaries with writeToFile.