I've got the array called listOfContactsToLay - it contains Contact objects with 3 string fields. How can I 开发者_开发技巧write this array to file successfully? Here what I've got:
-(NSString*)fPath
{
return [DocumentsDirectory() stringByAppendingPathComponent:@"quickdial.xml"];
}
//SERIALIZATION
- (void) save:(NSMutableArray*)array
{
[NSKeyedArchiver archiveRootObject:array toFile:[self fPath]];
NSLog(@"List of contacts been written in file");
}
// get that array back
- (NSMutableArray*)load
{
NSLog(@"List of contacts is being read from file");
return [NSKeyedUnarchiver unarchiveObjectWithFile:[self fPath]];
}
And of course the following doesn't work:
-(IBAction)doneWithContacts {
[ContactManager createXmlInMydocumentsDirectory];
[self save:listOfContactsToLay];
NSLog(@"List of contacts to lay saved!");
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
it throws invalid argument exception.
What should I do? how do I serialize the MSMutableArray of my objects? Thanks
Whether your Contact
class conforms to NSCoding
protocol.
More info.
Do you care about the format of the file? If not, why not use the built-in methods of the NSArray class?
+ (id)arrayWithContentsOfFile:(NSString *)aPath;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag;
See the NSArray Class Reference.
精彩评论