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

标签 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

最佳答案

至少你的 copyItemAtPath: 总是会失败,因为它不会覆盖文件(data2.plist 已经存在)。

关于iphone - 使用 NSMutableArray 的内容覆盖 plist 文件(plist 中的第一项始终为 NULL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8277092/

相关文章:

iphone - 相互比较数组元素,在重复项上将某个属性加在一起?

ios - NSThread isMainThread 异常

ios - 使用 Swift 的内存管理问题

html - 更新到10.3.1的iPhone不能再选择图片上传到网页,只能抓拍新的

ios - 为自定义 JSON 对象创建基类

iphone - willPresentActionSheet 一个类中的两个不同的操作表。如何知道哪一个会出现

iphone - 如何从一个 UITextField 获取值到另一个 UITextField ?

android - PayPal Mobile Express - 使用显示的信用卡支付而不是登录屏幕

ios - 将 View Controller 保持在与左侧相同的位置

ios - UIImagePickerController 不是全屏