 |
 |
using writeToFie: atomically:
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2002
Status:
Offline
|
|
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
|
|
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
|
|
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 encodeObject  ostionersArray 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
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum 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
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|