plist文件创建,储存,转化

创建.plist文件并存储

NSString *errorDesc;  //用来存放错误信息

NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件NSDictionary *innerDict;
NSString *name;
Player *player;
NSInteger saveIndex;
for(int i = 0; i < [playerArray count]; i++) {
player = nil;
player = [playerArray objectAtIndex:i];
if(player == nil)
break;
name = player.playerName;// This "Player1" denotes the player name could also be the computer name
innerDict = [self getAllNodeInfoToDictionary:player];
[rootObj setObject:innerDict forKey:name]; // This "Player1" denotes the person who start this game
}
player = nil;NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];红色部分可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码:
/*得到移动设备上的文件存放位置*/
NSString *documentsPath = [self getDocumentsDirectory];
NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
/*存文件*/
if (plistData) {
[plistData writeToFile:savePath atomically:YES];
}
else {
NSLog(errorDesc);
[errorDesc release];
}- (NSString *)getDocumentsDirectory {  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
return [paths objectAtIndex:0];  

}

读取plist文件并转化为NSDictionary

NSString *documentsPath = [self getDocumentsDirectory];

NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值