Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-15-2010, 01:39 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 15
Default 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
korki696 is offline   Reply With Quote
Old 03-15-2010, 12:12 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

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
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 03-15-2010, 03:12 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 15
Default

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
korki696 is offline   Reply With Quote
Old 03-15-2010, 03:30 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

Quote:
Originally Posted by korki696 View Post
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];
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Old 03-16-2010, 02:49 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 15
Default

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
korki696 is offline   Reply With Quote
Old 03-16-2010, 11:43 AM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,070
Default

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.
__________________
~~
-- Available Now: Dead Panic, a strategic zombie shooter!(iPhone)
-- New Blog Post: A Simple Observer Pattern for iPhone / Cocoa Games
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0