【问题标题】:How to use pList in iOS Programming如何在 iOS 编程中使用 pList
【发布时间】:2011-08-14 21:52:23
【问题描述】:

最近,我是一名高中生,我对制作 iPhone 应用程序很感兴趣。最近,我的一款应用出现了:NBlock。这是一个益智应用程序,非常具有挑战性。但是,它有一些问题。高分不保存。我被告知要使用 plist。有什么建议吗?

【问题讨论】:

标签: iphone objective-c ios cocoa-touch


【解决方案1】:

基于 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];

【讨论】:

  • 对我来说,使用 -[NSURL URLByAppendingPathComponent:] 代替相对 URL 似乎更清晰、更清晰。这将消除对路径百分比转义的需要,并且通常不易出错。
【解决方案2】:

我会避免使用 plist。到目前为止,在应用程序中保存简单数据的最简单方法是 NSUserDefaults

查看this tutorial 以获取有关如何使用NSUserDefaults 的简单指南。写完信后,一定要synchronizeNSUserDefaults

如果您正在寻找一种更强大(但更复杂)的数据保存方式,请查看 Apple 的Core Data 使用指南。

【讨论】:

  • 我认为 NSUserDefaults 被推荐用于您希望保留与语言偏好等相关的标准化默认设置?
【解决方案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。

【讨论】:

  • 电脑上的人可以请格式化我帖子中的源代码吗?在我的 ipod 上是不可能的......
  • 你这是什么意思? writeToURL:automatically:?
  • 但是,如果您实际上并没有写入 URL,为什么还需要 URL 方法呢??
  • 只是因为它是本地文件并不意味着它不是 URL。 IIRC 的建议是从基于路径的文件方法转向基于 URL 的方法。您的回答很好并且有效 - 我只是想展示另一种使用 URL 而不是路径来做同样事情的方法。
【解决方案4】:

查看 NSKeyedArchiver/Unarchiver。您可以保存几乎任何您想要的东西;根据我的经验,NSUserDefaults 会在您从托盘中杀死您的应用程序时转储您的数据。如果您使用 sqlite 等数据库管理大量数据,那么使用核心数据确实会更好。

【讨论】:

  • 我从来没有遇到过正确的synchronized NSUserDefaults 的问题。
  • 我会调查的,谢谢。 NSUserDefaults 只有在你只是保存一个简单的字符串或类似的东西时才真正有用(我猜提问者可能是一个很好的候选人),只要它可以永久保存。
  • 查看我在回答中添加的链接,了解如何处理该问题。
【解决方案5】:

除非使用自定义对象数据类型(这又是另一回事),否则我会说下面的代码可以正常工作并且非常简单:

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];
     }
   }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多