如何在 iOS 编程中使用 pList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7059966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use pList in iOS Programming
提问by Twinkle
Recently, I'm a senior in high school, and I'm interested in making apps for iPhone. Recently, one of my apps came out: NBlock. It's a puzzle app and it's very challenging. However, it has a few problems. The high scores are not saved. I've been told to use a plist. Any tips?
最近,我是一名高中生,我对为 iPhone 制作应用程序感兴趣。最近,我的一个应用程序出来了:NBlock。这是一个益智应用程序,非常具有挑战性。但是,它有一些问题。不保存高分。我被告知要使用 plist。有小费吗?
采纳答案by Abizern
The URL based method for this:
用于此的基于 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];
回答by mopsled
I'd avoid using a plist. The easiest way to save simple data in an application, by far, is NSUserDefaults
.
我会避免使用 plist。到目前为止,在应用程序中保存简单数据的最简单方法是NSUserDefaults
.
Check out this tutorialfor a simple guide on how to use NSUserDefaults
. Always be sure to synchronize
NSUserDefaults
when you're done writing to them.
查看本教程,了解如何使用NSUserDefaults
. 当你完成给他们写信时,一定要确保。synchronize
NSUserDefaults
If you're looking for a more powerful (but more complex) way to save data, check out Apple's guide to using Core Data
.
如果您正在寻找一种更强大(但更复杂)的数据保存方式,请查看 Apple 的使用指南Core Data
。
回答by JonasG
Heres what you want:
这是你想要的:
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];
And to add new scores do initWithContentsOfFile:@"scores.plist"
instead of init
in the declaration of array
. You can optionally use NSUserDefaults.
并添加新的分数,initWithContentsOfFile:@"scores.plist"
而不是init
在array
. 您可以选择使用 NSUserDefaults。
回答by Dylan Reich
Take a look into NSKeyedArchiver/Unarchiver. You can save pretty much anything you want; NSUserDefaults, in my experience, dumps your data if you kill your app from the tray. Core data is really used better if you're managing large amounts of data with databases such as sqlite.
看看 NSKeyedArchiver/Unarchiver。你几乎可以保存任何你想要的东西;根据我的经验,NSUserDefaults 会在您从托盘中杀死您的应用程序时转储您的数据。如果您使用 sqlite 等数据库管理大量数据,那么使用核心数据确实会更好。
回答by Dileep Mettu
I would say the below code will work and pretty straight forward unless custom object data types(Its a different story again) are used:
我会说下面的代码将工作并且非常简单,除非使用自定义对象数据类型(它又是一个不同的故事):
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];
}
}
setting the date attribute might be helpful to check when is the last time score was updated.
设置日期属性可能有助于检查上次更新分数的时间。