如何在iOS编程中使用pList

14

最近,我是一名高中生,对制作iPhone应用程序很感兴趣。最近,我的一个应用程序问世了:NBlock。它是一款智力游戏,非常具有挑战性。然而,它存在一些问题。最高分数没有保存下来。有人告诉我可以使用plist文件解决这个问题。你有什么建议吗?


4
你可以开始阅读苹果的plist教程(http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html)。 - Jon7
5个回答

12

通过URL的方式实现:

// Get the URL for the document directory
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *documentDirectoryURL = [[fileManager URLsForDocumentDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];

// Turn the filename into a string safe for use in a URL
NSString *safeString = [@"scores.plist" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Create an array for the score
NSMutableArray *array = [[NSMutableArray alloc] init];      
[array addObject:[NSNumber numberWithInt:score]];

// Write this array to a URL
NSURL *arrayURL = [NSURL URLWithString:safeString relativeToURL:documentDirectoryURL];
[array writeToURL:arrayURL atomically:YES];

1
对我来说,使用-[NSURL URLByAppendingPathComponent:]来处理文件URL似乎更加清晰和简洁,而不是使用相对URL。这将消除路径百分比转义的需要,并且通常更少出错。 - Nikolai Ruhe

3
这是您需要的内容:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"scores.plist"];
NSMutableArray *array = [[NSMutableArray alloc] init];      
[array addObject:[NSNumber numberWithInt:score]];        
[array writeToFile:path atomically:YES];

要添加新分数,请在声明array时使用initWithContentsOfFile:@"scores.plist"而不是init。您还可以选择使用NSUserDefaults。


1
有没有在电脑上的人能帮我把帖子中的源代码格式化一下?在我的iPod上做不了这件事... - JonasG
你是指 writeToURL:automatically: 是什么意思? - JonasG
2
但是如果您实际上并没有写入URL,为什么需要URL方法呢? - JonasG
仅仅因为它是本地文件并不意味着它不是URL。如果我没记错的话,建议是从基于路径的文件方法向基于URL的方法转移。你的回答很好并且有效——我只是想展示一种使用URL而非路径来完成相同任务的另一种方式。 - Abizern

3
我会避免使用 plist。到目前为止,在应用程序中保存简单数据的最简单方法是使用 NSUserDefaults。
请查看 这个教程 以获取有关如何使用 NSUserDefaults 的简单指南。在完成写入后,一定要 “同步” NSUserDefaults。
如果您正在寻找一种更强大(但更复杂)的保存数据的方式,请查看苹果公司使用 Core Data 的指南。

我认为NSUserDefaults被推荐用于标准化默认设置,这些设置与语言偏好等相关,并且您希望将其持久化? - cheznead

2

看一下NSKeyedArchiver/Unarchiver。你可以保存几乎任何想要的东西;根据我的经验,如果你从托盘中杀掉应用程序,NSUserDefaults会丢失你的数据。如果你使用像sqlite这样的数据库来管理大量数据,那么Core data会更好地被使用。


1
我从来没有遇到过使用正确的“同步”NSUserDefaults会出现这个问题。 - mopsled
1
我会研究一下,谢谢。NSUserDefaults只适合保存简单的字符串或类似的东西(我猜提问者可能是一个好的候选人),只要它可以永久保存。 - Dylan Reich
请查看我在答案中添加的链接,了解如何处理该问题。 - mopsled

0

我认为以下代码将有效且非常直观,除非使用自定义对象数据类型(这又是另一回事):

NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
 if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PathTo.plist"])) 
   {
    if ([manager isWritableFileAtPath:plistPath]) 
     {
       NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
      [infoDict setObject:@"foo object" forKey:@"fookey"];
      [infoDict writeToFile:plistPath atomically:NO];
      [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
     }
   }

设置日期属性可能有助于检查上次得分更新的时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接