plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:
以下代码在Mac和iPhone中均适用。
写入plist文件:
-
NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile : @ "/Sample.plist" ] ;
-
[ dict setObject : @ "Yes" forKey : @ "RestartSpringBoard" ] ;
-
[ dict writeToFile : @ "/Sample.plist" atomically : YES ] ;
读取plist文件:
-
NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile : @ "/Sample.plist" ] ;
-
NSString * object = [ dict objectForKey : @ "RestartSpringBoard" ] ;
//读plist 文件
到 NSMutableDictionary 中
dictplist = [[NSMutableDictionary alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
[dictplist setObject:@"testOne" forKey:@"key1"];
NSLog([dictplist objectForKey:@"key1"]);
NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
[dictplist setObject:array forKey:@"arrayitemtest"];
[dictplist writeToFile:plistPath atomically:YES];
NSString *name = [dictplist objectForKey:@"name" ];
NSLog(name);
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"arrayitemtest"]];
//从NSMutableDictionary 中构建 plist 文件
NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
dictplist = [[NSMutableDictionary alloc ] init];
[dictplist setObject:@"nameOne" forKey:@"name"];
[dictplist setObject:array forKey:@"item"];
[dictplist writeToFile:plistPath atomically:YES];
//arraylist = [[NSMutableArray alloc] init];
//[arraylist addObject:[dictplist objectForKey:@"name"]];
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"item"]];
dictplist = [[NSMutableDictionary alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
[dictplist setObject:@"testOne" forKey:@"key1"];
NSLog([dictplist objectForKey:@"key1"]);
NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
[dictplist setObject:array forKey:@"arrayitemtest"];
[dictplist writeToFile:plistPath atomically:YES];
NSString *name = [dictplist objectForKey:@"name" ];
NSLog(name);
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"arrayitemtest"]];
//从NSMutableDictionary 中构建 plist 文件
NSArray *array = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3",nil];
dictplist = [[NSMutableDictionary alloc ] init];
[dictplist setObject:@"nameOne" forKey:@"name"];
[dictplist setObject:array forKey:@"item"];
[dictplist writeToFile:plistPath atomically:YES];
//arraylist = [[NSMutableArray alloc] init];
//[arraylist addObject:[dictplist objectForKey:@"name"]];
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"item"]];