 |
 |
|
 |
03-15-2010, 01:39 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 15
|
Saving NSArray to file with custom objects?
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:
Code:
array = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:
[[[TTLauncherItem alloc] initWithTitle:@"Game1"
image:@"bundle://Icon.png"
URL:@"tt://game1" canDelete:NO] autorelease],
[[[TTLauncherItem alloc] initWithTitle:@"Game2"
image:@"bundle://Icon.png"
URL:@"tt://game2" canDelete:NO] autorelease],
[[[TTLauncherItem alloc] initWithTitle:@"Game3"
image:@"bundle://Icon.png"
URL:@"tt://game3" canDelete:NO] autorelease],
[[[TTLauncherItem alloc] initWithTitle:@"Game4"
image:@"bundle://Icon.png"
URL:@"tt://game4" canDelete:NO] autorelease],
[[[TTLauncherItem alloc] initWithTitle:@"Game5"
image:@"bundle://Icon.png"
URL:@"tt://game5" canDelete:NO] autorelease],
nil],
nil
];
So how could I save this array in a file?
Thanks
-Korki
|
|
|
03-15-2010, 12:12 PM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
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:
Code:
[NSKeyedArchiver archiveRootObject: array toFile:filePath];
Here's a tutorial on how to make your objects conform to NSCoding.
How to save your game (or any object) on the iPhone | Dead Panic
|
|
|
03-15-2010, 03:12 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 15
|
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
|
|
|
03-15-2010, 03:30 PM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
Quote:
Originally Posted by korki696
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:
Code:
[self initWithTitle: savedTitle
image: savedImage
URL: saveURL];
|
|
|
03-16-2010, 02:49 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 15
|
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
Code:
[[[TTLauncherItem alloc] initWithTitle:@"Game1"
image:@"bundle://Icon.png"
URL:@"tt://game1" canDelete:NO] autorelease]
Thanks again
-Korki
|
|
|
03-16-2010, 11:43 AM
|
#6 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
|
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:
Code:
myArray = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath];
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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 425 |
| 34 members and 391 guests |
| AdamSubach, aderrington, benoitr007, bensj, BrianSlick, Danneman, dev123, ErichGS, GreatWizard, gustavo7sexton, gw1921, HemiMG, iSDK, Jeremy1026, lifeCoder45, melodizzzy, mriphoneman, newchucky, Ovidius, Piequanna, pofak, qilin, Racker, raheel, rendezvouscp, riq, Sega dude, socals, timle8n1, Whitehk, ZunePod |
| Most users ever online was 965, 06-30-2010 at 04:26 AM. |
» Stats |
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
|
| Welcome to our newest member, melodizzzy |
|