【发布时间】:2010-04-27 15:09:19
【问题描述】:
已编辑。
嘿,我正在尝试将 NSMutableArray 写入 plist。 编译器没有显示任何错误,但无论如何它都不会写入 plist。 我也在真机上试过,不只是模拟器。
基本上,这段代码的作用是,当您单击UITableViewCell 的accessoryView 时,它会按下indexPath,编辑NSMutableArray 并尝试将NSMutableArray 写入plist。然后它重新加载提到的数组(从多个 plist 中)并从数组中重新加载 UITableView 中的数据。
代码:
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];
CFShow() 在数组之后删除其中一个显示:
<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}
DEBUG-INFO:writeToPlist 显示 YES,我尝试在再次填充之前释放所有数组,将它们设置为 nil,将 atomically 设置为 NO。
【问题讨论】:
-
你为什么要使用
numberWithInt:创建一个NSNumber然后调用intValue呢?为什么不直接使用 int...? -
因为
indexOfObject:需要一个对象。 -
请先显示您在哪里创建对象并将对象添加到
arrayFav的代码?很可能,对象包含某些与 plist 不兼容的类型。 -
NSString *plistFavPath = [[NSBundle mainBundle] pathForResource:@"arrayFav" ofType:@"plist"]; arrayFav = [[NSMutableArray alloc] initWithContentsOfFile:plistFavPath];
-
返回 YES 表示已成功写入文件。为什么你认为没有?您是否在模拟器上运行它并查看应用程序文件夹中的 plist 文件?问题可能在于您对文件的阅读而不是写作。
标签: iphone objective-c nsmutablearray plist