首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据写入Plist

将数据写入Plist
EN

Stack Overflow用户
提问于 2012-10-04 13:37:14
回答 1查看 144关注 0票数 1

我有一个plist,其中包含一个字典,比如dict,在dict中,我有3个数组用于键A,B,C。现在我想在A,B和C中添加一个特殊情况下的项。为了这个我这样做。

代码语言:javascript
复制
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingString:@"MovingChecklist.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    if (![fileManager fileExistsAtPath: plistPath]) 
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"MovingChecklist" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath: plistPath error:&error];
    }
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *array1 = [dict objectForKey:@"A"];
    NSArray *array2 = [dict objectForKey:@"B"];
    NSArray *array3 = [dict objectForKey:@"C"];

    NSMutableArray *myArray;
    if (currentMovingList == KMovingListTypeA) {
        myArray = [NSMutableArray arrayWithArray:array1];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"A"];

    }
    if (currentMovingList == KMovingListTypeB) {
        myArray = [NSMutableArray arrayWithArray:array2];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"B"];
    }
    if (currentMovingList == KMovingListTypeC) {
        myArray = [NSMutableArray arrayWithArray:array3];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"C"];
    }
[dict writeToFile:plistPath atomically: YES];

但plist并没有改变。告诉我我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-04 13:51:03

我认为您应该将它设置为可变的

代码语言:javascript
复制
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12720884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档