改写了NSMutableArray的内容的plist文件(第一项是在plist中总是NULL)
问题描述:
- 我想使用的plist文件从一个文本文件 从网站上保存的项目清单。当我第一次创建plist文件并添加 项目时,没有问题。但是,当我尝试从plist删除项目 时,它不会删除索引,它只会用NULL覆盖此索引的 内容。我尝试了另一种方式;我试图 创建一个没有我想要删除的项目的新阵列,并用这个新阵列的内容覆盖 plist文件。通过这种方式,我想删除的项目 被删除,但令人惊讶的是,第一项获得了NULL!更令人惊讶的情况是,我也用相同的技术将它写入新的plist 文件,并且它是完美的!这是一个非常原始的代码,不幸的是它没有为我工作。我搜索了 大量的教程,但我无法克服。如何将字符串数组的 内容写入到plist文件中,而不需要额外的空对象 并且不会丢失数据?
============================================ ============================改写了NSMutableArray的内容的plist文件(第一项是在plist中总是NULL)
我由以下示例代码:
- (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.
}
}
答
至少你copyItemAtPath:
总是会失败,因为它不会覆盖文件(data2.plist已经存在)。
但是在我用这个命令删除文件data2.plist之前:[fileManager removeItemAtPath:[NSHomeDirectory()stringByAppendingPathComponent:@“Documents/data2.plist”] error:nil]; – ozgunb
真的吗?在你的代码中,我看到你先删除data2,然后写入data2,然后写入data3,然后尝试将data3复制到data2。 – mifki
对不起,我缺乏解释。我的意思是,我试图删除该行:[arr writeToFile:[NSHomeDirectory()stringByAppendingPathComponent:@“Documents/data2.plist”] atomically:NO];所以首先将它写入data3.plist,然后尝试创建data2.plist,但结果相同。 – ozgunb