属性列表(Plist存储)

属性列表是一种XML格式的文件,拓展名为plist

如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中

注意点:Plist不能存储自定义对象

- (IBAction)save:(id)sender {
    NSArray *arr = @[@"ffff",@1,@{@"vvv":@"111"}];

    NSString *filePath = [self CachesWtihFilePath:@"arr.plist"];
    NSLog(@"%@",filePath);

    [arr writeToFile:filePath atomically:YES];

}

- (IBAction)get:(id)sender {

    NSString *filePath = [self CachesWtihFilePath:@"arr.plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];

    NSLog(@"%@",arr);

}

-(NSString *)CachesWtihFilePath:(NSString *)plistPath{
    //atomically参数意思是如果为YES则保证文件的写入原子性,就是说会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.
    //如果为NO,则直接写入目标文件里.
    NSString *Caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return [Caches stringByAppendingPathComponent:plistPath];
}

results matching ""

    No results matching ""