0

我有一个表视图,其中包含从 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];
}

}

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{

NSString *contentsToMove = [[[self categoryList] objectAtIndex:[fromIndexPath row]] retain];
[[self categoryList] removeObjectAtIndex:[fromIndexPath row]];
[[self categoryList] insertObject:contentsToMove atIndex:[toIndexPath row]];
NSArray *newArr = [[NSArray alloc]initWithArray:self.categoryList];
[newArr writeToFile:self.categoryFile atomically:YES];
[newArr release];
[contentsToMove release];
}

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

4

3 回答 3

0

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

改编自这个问题的答案的代码:

- (NSString *)pathToPlist
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Categories.plist"];
    BOOL copyProblem;
    if ([fileManager fileExistsAtPath:plistPath] == NO) {
        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"];
        copyProblem = [fileManager copyItemAtPath:resourcePath toPath:plistPath error:&error];
    }

    if (copyProblem)
        return nil;

    return plistPath
}

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

于 2012-09-28T09:18:26.160 回答
0

如果您想稍后编辑和保存文件,您必须先复制 NSDocumentsDirectory 中的文件(如果它不存在)。请参阅此链接以供参考

用于加载您的 plist

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];


}

用于保存 plist

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];
于 2012-09-28T09:18:36.670 回答
0

您要做的是从 [NSBundle mainBundle] 中删除一个文件,这永远不会发生,因为这些文件是只读的..

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

试试这个链接以供参考

于 2012-09-28T09:19:41.997 回答