Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 编辑表格视图并更新plist_Iphone_Ios_Uitableview_Plist_Objective C Blocks - Fatal编程技术网

Iphone 编辑表格视图并更新plist

Iphone 编辑表格视图并更新plist,iphone,ios,uitableview,plist,objective-c-blocks,Iphone,Ios,Uitableview,Plist,Objective C Blocks,我有一个表视图,其中包含从plist加载的类别名称列表。 我想动态删除、添加和重新排序列表,并将其保存到plist。我使用了下面的代码,它在模拟器上运行良好,但在设备上崩溃 In ViewDid Load: self.categoryFile = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"plist"]; self.categoryList = [[NSMutableArray alloc]initWithCon

我有一个表视图,其中包含从plist加载的类别名称列表。 我想动态删除、添加和重新排序列表,并将其保存到plist。我使用了下面的代码,它在模拟器上运行良好,但在设备上崩溃

In ViewDid Load:
 self.categoryFile = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"plist"];
 self.categoryList = [[NSMutableArray alloc]initWithContentsOfFile:self.categoryFile];

- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
    [[self categoryList] removeObjectAtIndex:[indexPath row]];
    NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];
    [tableView reloadData];
    NSArray *newArr = [[NSArray alloc]initWithArray:self.categoryList];
    [newArr writeToFile:self.categoryFile atomically:YES];
    [newArr release];
}
}


错误出现在writeToFile:atomically行。如果我删除该行,它也会在设备上工作,但没有任何用处。

您无法写入设备上的捆绑资源。当您第一次需要plist时,将其复制到应用程序的documents目录中,并从此读取/更新它

代码改编自


将其分配给self.categoryFile,而不是调用NSBundle。代码未编译或测试,因此应视为详细的psuedocode,请准备更正小的打字错误。

如果以后要编辑和保存文件,必须先在NSDocuments目录中复制该文件(如果不存在)。看到这个了吗

为了装你的钱包

BOOL success;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:@"your_file" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];

success = [fileManager fileExistsAtPath:path];
if (success) {
    self.selectedObjectsDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];       

}
else
{    

    success = [fileManager copyItemAtPath:filePath  toPath:path error:&error];
    self.selectedObjectsDict=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath];


}
为了拯救穷人

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];
[your_dict writeToFile:path atomically:YES];

您试图做的是从[NSBundle mainBundle]中删除一个文件,因为这些文件是只读的,所以永远不会发生这种情况

如果要删除和创建文件,请在DocumentsDirectory中尝试


试试这个作为参考

你能给我一些代码吗,因为我不知道你在说什么,而且我是新来的。每个应用程序都有一个只读包,然后是一个读写沙盒,其中包括一个文档目录。如果要在应用程序中写入文件,可以将文件从捆绑包复制到文档目录。代码应该足够容易挖掘up@Chandu你能在这方面取得进展吗?我能解释一下答案吗?
BOOL success;
NSError *error;
NSString *filePath= [[NSBundle mainBundle] pathForResource:@"your_file" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];

success = [fileManager fileExistsAtPath:path];
if (success) {
    self.selectedObjectsDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];       

}
else
{    

    success = [fileManager copyItemAtPath:filePath  toPath:path error:&error];
    self.selectedObjectsDict=[[NSMutableDictionary alloc]initWithContentsOfFile:filePath];


}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"your_file.plist"]];
[your_dict writeToFile:path atomically:YES];