编辑。
嘿,我正在尝试将一个NSMutableArray写入plist。 编译器不会显示任何错误,但无论如何它都不会写入plist。 我也在真实的设备上试过这个,而不仅仅是模拟器。
基本上,此代码的作用是,当您点击accessoryView
的{{1}}时,会按下UITableViewCell
,编辑indexPath
并尝试编写那个NSMutableArray
到了一个plist。然后重新加载提到的数组(来自多个plist)并从数组中重新加载NSMutableArray
中的数据。
代码:
UITableView
删除其中一个后,数组上的CFShow()显示:
NSIndexPath *indexPath = [table indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:table]];
[arrayFav removeObjectAtIndex:[arrayFav indexOfObject:[NSNumber numberWithInt:[[arraySub objectAtIndex:indexPath.row] intValue]]]];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"arrayFav.plist"];
NSLog(@"%@ - %@", rootPath, plistPath);
[arrayFav writeToFile:plistPath atomically:YES];
// Reloads data into the arrays
[self loadDataFromPlists];
// Reloads data in tableView from arrays
[tableFarts reloadData];
DEBUG-INFO:<CFArray 0x6262110 [0x2c810a0]>{type = mutable-small, count = 4, values = (
0 : <CFNumber 0x6502e10 [0x2c810a0]>{value = +3, type = kCFNumberSInt32Type}
1 : <CFNumber 0x6239de0 [0x2c810a0]>{value = +8, type = kCFNumberSInt32Type}
2 : <CFNumber 0x6239dc0 [0x2c810a0]>{value = +10, type = kCFNumberSInt32Type}
3 : <CFNumber 0x6261420 [0x2c810a0]>{value = +40, type = kCFNumberSInt64Type}
显示YES,我尝试在再次填充之前释放所有数组,将它们设置为writeToPlist
,将nil
设置为NO。
答案 0 :(得分:3)
[yourMutableArray writeToFile:fileName atomically:YES];
这应该有效。 NSMutableArray继承自NSArray,其中method写入plist。
答案 1 :(得分:3)
正如下面的评论中所讨论的,这里的实际问题是正在从两个不同的位置读取和写入plist。在应用程序的某个地方,有代码将文件读入数组,类似于:
NSString *plistFavPath = [[NSBundle mainBundle] pathForResource:@"arrayFav"
ofType:@"plist"];
arrayFav = [[NSMutableArray alloc] initWithContentsOfFile:plistFavPath];
此逻辑从应用程序的包中读取数组,该包是一个只读位置,是分布式应用程序的一部分。稍后当编辑的数组被持久化时,使用类似于此的代码:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *plistPath = [rootPath
stringByAppendingPathComponent:@"arrayFav.plist"];
NSLog(@"%@ - %@", rootPath, plistPath);
[arrayFav writeToFile:plistPath atomically:YES];
这里的结果是更新的文件被写入应用程序的文档目录,但是从那里读取它,从而给出了文件没有正确保存的外观。要更正此问题,您应该更改读取文件的代码以使用与您编写的路径相同的路径。
如果您需要在编辑阵列之前分发初始启动时使用的plist的默认版本,您可以继续在捆绑包中包含该文件的一个版本,然后将代码添加到您的应用代理中如果文件存在于文档目录中,并且该文件不存在,则将该文件包的默认版本复制到正确的位置。
答案 2 :(得分:1)
writeToFile:atomically:
将无效。
如果您的数组包含不是Plist对象的自定义对象(NSArray,NSDictionary,NSString,NSNumber等),那么您将无法使用此方法。此方法仅适用于Plist对象。
另一种选择是使用NSCoding协议,并以这种方式将对象写入磁盘。
答案 3 :(得分:0)
是
查看Property List Programming Guide。
phoneNumbers是一个NSMutableArray
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: personName, phoneNumbers, nil]
forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(error);
[error release];
}
return NSTerminateNow;
}