作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将我创建的 .plist
保存到我的 NSUserDefaults
中,这样我就可以保存我放入其中的数据,所以如果应用程序是停止(从多任务栏中删除)我不会丢失这些值。
我已被指向本教程 here
里面有这个示例代码。
-(void)saveToUserDefaults:(NSString*)myString
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:@"Prefs"];
[standardUserDefaults synchronize];
}
}
-(NSString*)retrieveFromUserDefaults
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:@"Prefs"];
return val;
}
我需要帮助的是上述内容与保存 .plists
有何关系?
我有一个 .plist
Controller 类,它读取包 .plist
在根文档中创建一个新的 .plist
然后读写到那......我怎么用上面的来保存它当应用程序退出还是关闭?
这就是我加载和写入我的 .plist
的方式,目前使用单例并且它在自己的类中。
#pragma mark Singleton Methods
+ (id)sharedManager {
@synchronized(self) {
if (sharedMyManager == nil)
sharedMyManager = [[self alloc] init];
}
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
// Data.plist code
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"EngineProperties" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *tempRoot = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!tempRoot)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
// assign values
self.signature = [tempRoot objectForKey:@"Signature"];
self.version = [tempRoot objectForKey:@"Version"];
self.request = [tempRoot objectForKey:@"Request"];
self.dataVersion = [tempRoot objectForKey:@"Data Version"];
man = [cacheValue objectForKey:@"Man"];
mod = [cacheValue objectForKey:@"Mod"];
sub = [cacheValue objectForKey:@"SubMod"];
cacheValue = [tempRoot objectForKey:@"Cache Value"];
}
- (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue;
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"];
// set the variables to the values in the text fields
self.signature = pSignature;
self.version = pVersion;
self.request = rNumber;
self.dataVersion = dvReturned;
//do some if statment stuff here to put the cache in the right place or what have you.
if (methodName == @"manufacturers")
{
self.man = cValue;
}
else if (methodName == @"models")
{
self.mod = cValue;
}
else if (methodName == @"subMod")
{
self.sub = cValue;
}
self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys:
man, @"Manufacturers",
mod, @"Models",
sub, @"SubModels", nil];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys:
signature, @"Signature",
version, @"Version",
request, @"Request",
dataVersion, @"Data Version",
cacheValue, @"Cache Value", nil];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];
// NSLog(@"%@", myString);
}
else
{
NSLog(@"Error in saveData: %@", error);
// [error release];
}
}
@end
最佳答案
你不能将 .plist
写入 NSUserDefaults
,至少实际上不行。为此,您必须将 .plist
中的特定键写入 NSUserDefaults
,这基本上就像保存所有数据的两个副本。您可以将 NSUserDefaults
视为一个不可见的 .plist
,您可以对其进行读取和写入,而无需实际查看该文件。使用 NSUserDefaults
,即使应用在多任务处理中被终止,您也可以恢复保存的值。
但是,您如何在 .plist
和 NSUserDefaults
之间进行选择应该基于您需要保存多少数据。 Apple 建议只将少量数据保存到 NSUserDefaults
。如果您需要保存大量信息,那么 .plist
是最佳选择。或者当然是 Core-Data
。
关于iphone - 如何将 plist 保存到 NSUserDefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098512/
我是一名优秀的程序员,十分优秀!