Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > using writeToFie: atomically:

using writeToFie: atomically:
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Sep 19, 2003, 03:08 PM
 
I'm trying to understand when to use writeToFile: atomically and it says that i can only be used for objects of type NSDictionary, NSArray, NSString.

Will this method not work of writing to a file if the object is an NSArray that contains my model object? My model object is a class that has an NSArray, integer, and 2 NSString types.

I want to be able to save my data in an XML format.

What is the most simplest way to save data in an XML format?

Thanks.
     
Forum Regular
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 19, 2003, 03:48 PM
 
to save a custom object, you would need to implement the NSCoder protocol for your object. essentially, you need to define how the instance variables in your object should be represented in data stream, which could then be written to a file. however, this may be a bit much for what you want to do. as you mentioned, objects like NSDictionary, NSArray, NSNumber, NSDate, NSString, etc. can already be used to write out to a file. A NSDictionary or NSArray is written out as a XML Apple Property List (plist) and can contain any of the above elements. you might check our NSPropertyListSerialization to see more about what types are supported.

you could represent you custom object as a collection of supported objects.

NSArray
|
|\ _ NSArray (NSArray, NSNumber, NSString, NSString)
|
|\_ NSArray (NSArray, NSNumber, NSString, NSString)
|
etc.

and then write some converter methods in your custom class that would initialize one of your custom objects from an array, and also return an array representing itself. either way would work, i think, so you will just have to see which would work best for your case.

kido
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Sep 19, 2003, 05:50 PM
 
Hi kido,

Thanks.
I'm currently reading key value coding using NSKeyedArchiver and NSKeyedUnarchiver and I tried implementing it and I get a EXC_BAD_ACCESS when attempting a

[coder encodeObjectostionersArray forKey:@"positionersArray"];

It seems that key value coding only seems to work if the array or dictionary's content is of a string or some object c type.

I would really like to know how to store my data in an XML format.

Thanks.

ron
Originally posted by kido:
to save a custom object, you would need to implement the NSCoder protocol for your object. essentially, you need to define how the instance variables in your object should be represented in data stream, which could then be written to a file. however, this may be a bit much for what you want to do. as you mentioned, objects like NSDictionary, NSArray, NSNumber, NSDate, NSString, etc. can already be used to write out to a file. A NSDictionary or NSArray is written out as a XML Apple Property List (plist) and can contain any of the above elements. you might check our NSPropertyListSerialization to see more about what types are supported.

you could represent you custom object as a collection of supported objects.

NSArray
|
|\ _ NSArray (NSArray, NSNumber, NSString, NSString)
|
|\_ NSArray (NSArray, NSNumber, NSString, NSString)
|
etc.

and then write some converter methods in your custom class that would initialize one of your custom objects from an array, and also return an array representing itself. either way would work, i think, so you will just have to see which would work best for your case.

kido
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Sep 19, 2003, 06:40 PM
 
Check out this documentation from Apple. It's pretty easy to do.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Dedicated MacNNer
Join Date: Feb 2001
Status: Offline
Reply With Quote
Sep 22, 2003, 04:02 PM
 
As others have said, you can use NSCoder, but I personally don't like that because you end up with opaque binary data in a proprietary format. What I do is have all my model classes that I want to serialize implement -propertyList and -initFromPropertyList: methods. -propertyList returns an NSDictionary that can be persisted using -writeToFile:atomically: , and read back in and reinstantiated using -initFromPropertyList: . This isn't much more work than using NSCoder, produces human-readable XML output, and has the same benefits of keyed archiving in that it can easily deal with new instance variables being added.
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Sep 24, 2003, 02:29 PM
 
Hi 3.1416,

Can you direct me to where I can view some sample code on how to implement the propertyList and intiFromPropertyList methods
to store my data as readable XML?

Thanks...


Originally posted by 3.1416:
As others have said, you can use NSCoder, but I personally don't like that because you end up with opaque binary data in a proprietary format. What I do is have all my model classes that I want to serialize implement -propertyList and -initFromPropertyList: methods. -propertyList returns an NSDictionary that can be persisted using -writeToFile:atomically: , and read back in and reinstantiated using -initFromPropertyList: . This isn't much more work than using NSCoder, produces human-readable XML output, and has the same benefits of keyed archiving in that it can easily deal with new instance variables being added.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 07:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2