从文档文件iOS中检索数据

时间:2012-08-10 05:54:43

标签: iphone ios xcode ipad plist

如何获取

下保存的文件
-(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添加项目?

1 个答案:

答案 0 :(得分: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];