Reputation: 2233
I have been trying to do this for a week now... with no luck!
Object book:
String ISBN
int NumOfPages
String Title
String Author (etc.)
Object Library:
MuttableArray books
Now I need to write a method for the Object of class Library to write all the information of all the books in XML to the disk. This XML data is also needed to be sent over the network to the server. The server is running JAVA and I need to be able to work with Objects of classes Library and Book on the server too.
I here is what I have done up till now:
Class Book:
@interface Book : NSObject <NSCoding>
@property NSString * title;
@property NSString * author;
@property int * numOfPages;
@end
Class Library:
@interface Library : NSObject
@property NSMuttableArray * Books;
-(void) syncLibraryToFile;
@end
Upvotes: 1
Views: 2360
Reputation: 28419
The default archiver generates binary. If you want XML, you have to change the output format.
NSMutableData *archiveData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:archiveData];
archiver.outputFormat = NSPropertyListXMLFormat_v1_0;
[archiver encodeRootObject:self.Books];
[archiver finishEncoding];
Now you can send your archiveData
to the server. If you want JSON instead, it's easier still, and does not require a third-party framework.
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.Books
options:0
error:&error];
There are many options for writing data to disk, including the NSData
methods
– writeToFile:atomically:
– writeToFile:options:error:
– writeToURL:atomically:
– writeToURL:options:error:
EDIT
In response to the question in the comments... you can easily write the data to a file...
[archiveData writeToFile:filePath atomically:YES];
or
[jsonData writeToFile:filePath atomically:YES];
Upvotes: 3