属性列表使用非常方便,可以将NSDictionary和NSArray实例写入属性列表及创建他们。

 

1、获取属性列表

  

- (NSString *)dataFilePath

{

//获取Documen目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//获取Temp目录

//NSString *paths=NSTemporaryDirectory();

    NSString *documentsDirectory = [paths objectAtIndex:0];


//返回文件的路径

    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

}

 

 

 

 

 

2、获取属性列表数据

 

//获取属性表的数据

    NSString *filePath = [self dataFilePath];

//判断文件是否存在

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])

{

        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

        field1.text = [array objectAtIndex:0];

        field2.text = [array objectAtIndex:1];

        field3.text = [array objectAtIndex:2];

        field4.text = [array objectAtIndex:3];

        [array release];

    }

 

 

 

3、向属性更表添加数据

 

 

    NSMutableArray *array = [[NSMutableArray alloc] init];

    [array addObject:field1.text];

    [array addObject:field2.text];

    [array addObject:field3.text];

    [array addObject:field4.text];

    [array writeToFile:[self dataFilePath] atomically:YES];

    [array release];