我有一個名爲listOfContactsToLay的數組 - 它包含具有3個字符串字段的Contact對象。我怎樣才能將這個數組寫入文件? 這裏我已經有了:iPhone dev - 將對象數組寫入/讀取到文件
-(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]];
}
,當然還有以下不工作:
-(IBAction)doneWithContacts {
[ContactManager createXmlInMydocumentsDirectory];
[self save:listOfContactsToLay];
NSLog(@"List of contacts to lay saved!");
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
它拋出無效參數異常。
我該怎麼辦?我如何序列化我的對象的MSMutableArray? 謝謝
如果陣列中的內容都是屬性列表對象(的NSString,NSData的,NSArray的,或者NSDictionary的對象),此方法寫入的文件可以被用來初始化一個新的陣列類方法arrayWithContentsOfFile:或實例方法initWithContentsOfFile :.此方法在寫出文件之前遞歸地驗證所有包含的對象是屬性列表對象,並且如果所有對象都不是屬性列表對象,則返回NO,因爲生成的文件不是有效的屬性列表。 –