Thursday, May 2, 2013

NSKeyedArchiver outputFormat example ios


setOutputFormat:

Sets the format in which the receiver encodes its data.
- (void)setOutputFormat:(NSPropertyListFormat)format
Parameters of [NSKeyedArchiver setOutputFormat]
format
The format in which the receiver encodes its data. format can beNSPropertyListXMLFormat_v1_0 or NSPropertyListBinaryFormat_v1_0.
Example of [NSKeyedArchiver setOutputFormat]
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archiver encodeObject:artistCollection forKey:@"root"];
[archiver finishEncoding];
[data writeToFile:@"/Users/fgx/Desktop/stuff" atomically:YES];
[archiver release];
Example of [NSKeyedArchiver setOutputFormat]
NSDictionary *dataAsDictionary=[self toDictionaryBlockingRelationships:blockRelationship];
NSString   *savePath = [@"~/Documents/Saved.data" stringByExpandingTildeInPath];
NSMutableData *xmlData=[NSMutableData data];

NSKeyedArchiver *archive=[[NSKeyedArchiver alloc ]initForWritingWithMutableData:xmlData];

[archive setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archive encodeRootObject:dataAsDictionary];
[archive finishEncoding];

if(![xmlData writeToFile:savePath atomically:NO]){
    NSLog(@"Failed to write to file to filePath=%@", savePath);
}
[archive release];