1

我如何获取保存在下的文件

-(NSString *) dataFilePath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"Answer.plist"];
}

NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    { NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        NSLog(@"%@\n",array);
        NSLog(@"%@\n", filePath);
    }

有什么方法可以将项目添加到 plist 而不覆盖它?

4

1 回答 1

1

你可以使用这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0]; 
        path = [documentsDirectory stringByAppendingPathComponent:@"Answer.plist"];

        NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
        NSLog(@"array, %@",array);
        [array removeObjectAtIndex:yourIndex];

或者

[array addObjectAtIndex:yourIndex];

[array writeToFile:path atomically:YES];
于 2012-08-10T06:02:39.207 回答