使用NSMutableArray的内容覆盖plist文件(第一项在plist中始终为NULL)

时间:2011-11-26 07:54:28

标签: iphone objective-c ios xcode

  • 我正在尝试使用plist文件来保存文本中的项目列表 来自网站的文件。当我第一次创建plist文件并添加时 物品到那,没有问题。但是当我尝试删除一个项目时 从plist,它不是删除索引,它只覆盖了 此索引的内容为NULL。我尝试了另一种方式;我试过了 创建一个没有我要删除的项目的新数组,并覆盖 plist文件包含此新数组的内容。这样,项目 我想删除被删除,但令人惊讶的是第一项获得 空值!更令人惊讶的是,我还把它写给了一个新的plist 使用相同技术的文件,它是完整的!这是非常的 原始代码,不幸的是它对我没用。我搜索了 很多教程,但我无法克服。我该怎么写呢 字符串数组的内容到没有额外空对象的plist文件 并且没有丢失数据?

=============================================== =========================

我在下面编写了一个示例代码:

- (IBAction)logFromPlist{

    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"]];

    NSLog(@"LOG:");
    NSLog(@"arrplist count : %d", [arr count]);
    for(int a=0; a<[arr count]; a++){
        NSLog(@"*** %@", [arr objectAtIndex:a]);
    }
}

- (IBAction)logFromPlist2{

    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data3.plist"]];

    NSLog(@"LOG:");
    NSLog(@"arrplist count : %d", [arr count]);
    for(int a=0; a<[arr count]; a++){
        NSLog(@"*** %@", [arr objectAtIndex:a]);
    }
}

- (IBAction)addValue{

    NSString *deger = [field5 text];  //New value text field in IB
    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"]];
    if(arr == NULL){
        arr = [[NSMutableArray alloc] init];
    }
    [arr addObject:deger];
    [arr writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"] atomically:NO];
}

- (IBAction)removeFromPlist{

    NSMutableArray *arr2 = [[NSMutableArray alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"]];

    if(arr2 != NULL){
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        NSString *key = [field8 text];

        for(int i = 0; i < [arr2 count]; i++){
            NSString *cntStr = [[NSNumber numberWithInt:i] stringValue];
            if(![cntStr isEqualToString:key]){
                NSString *tempDeger = [arr2 objectAtIndex:i];
                if(tempDeger != NULL){
                    [arr addObject:tempDeger];
                }else{
                    NSLog(@"it is NULL");
                }
            }
        }

        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"] error:nil];   //I tried this line by removing next line

        [arr writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"] atomically:NO];   //It is writing the array to plist but first item is always null

        [arr writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data3.plist"] atomically:NO];   //same technique but everything is ok in this plist


        [fileManager copyItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data3.plist"] toPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data2.plist"] error:nil];   // trying to copy correct plist file (data3.plist) to original plist file (plist2), but it does not fix the problem.

    }
}

项目文件:http://www.ozgunbursalioglu.com/files/plistWork.zip

2 个答案:

答案 0 :(得分:0)

尝试通过将自动设置为YES

来编写文件
 [arr writeToFile:PATH atomically:YES]; 

并且还尝试检查返回的BOOL值以查看您的运行是否成功完成

答案 1 :(得分:0)

至少您的copyItemAtPath:将始终失败,因为它不会覆盖文件(data2.plist已经存在)。