Objective-C学习笔记-plist文件

1.苹果系统中大量使用了plist文件来保存配置信息,plist文件由以下对象组成

  • NSArray
  • NSDictionary
  • NSString
  • NSData
  • NSDate
  • NSNumber

2.通过NSArray和NSDictionary的内建方法可以读写plist文件,代码如下

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //写入plist
        NSMutableArray *writeData=[[NSMutableArray alloc] init];
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:@"value123" forKey:@"key1"];
        [dic setObject:[NSNumber numberWithInt:200] forKey:@"key2"];
        [writeData addObject:dic];
        [writeData writeToFile:@"/tmp/test.plist" atomically:YES];

        //读取plist
        NSArray *readData=[NSArray arrayWithContentsOfFile:@"/tmp/test.plist"];
        for (NSDictionary *d in readData){
            NSLog(@"value1=%@,value2=%@",[d objectForKey:@"key1"],[d objectForKey:@"key2"]);
        }
    }
    return 0;
}

你可能感兴趣的:(Objective-C学习笔记-plist文件)