读取和写入plist文件

时间:2012-03-27 20:37:32

标签: objective-c ios cocos2d-iphone plist

我需要写一个plist文件,然后再读出数据...... 但是,每当我测试我的应用程序时,写入plist文件的数据在我退出iphone模拟器后永远不会保存!你能告诉我为什么这个问题存在的原因吗?在运行时,数据似乎写得很好并且读得很好。但我的plist文件永远不会更新! 我可能做错了。那么请你指导我在cocos2d中创建一个plist并读取和写入整数吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

保存:

+ (void)save:(Example *)exampleobject {
     NSString * directory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString * path = [directory stringByAppendingPathComponent:@"Example.plist"];
     [NSKeyedArchiver archiveRootObject:exampleobject toFile:path];
}

要检索

+ (Example *)load {
     NSString * directory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString * path = [directory stringByAppendingPathComponent:@"Example.plist"];
     NSData * data = [NSData dataWithContentsOfFile:path];
     Example * exampleobject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
     if (!exampleobject) { exampleobject = [[Example alloc] init]; }
     return exampleobject;
}