我正在创建一个plist来保存一些值,但是在测试期间我已经注意到,在应用程序关闭后,我保留了我新创建的plist &从多任务中删除。然而,如果应用程序从多任务处理中删除,但如果应用程序已关闭,则我将失去该plist内的值...保留plist但丢失plist值
这是保存数据方法,我在我的plist控制器类中管理所有读/写/保存等
- (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 == @"manu")
{
self.man = cValue;
}
else if (methodName == @"models")
{
self.mod = cValue;
}
else if (methodName == @"subMod")
{
self.sub = cValue;
}
self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys:
man, @"Manu",
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
我的问题有两个部分.. 我能否拯救值,使其保持在即使该应用程序从multitaksing栏中删除plist中。 如果可以工作,需要更改以使其工作?
可能不是问题,但您可以在字典上调用writeToFile:并将其作为plist输出。 – 2012-04-11 02:57:07
您正在保存文件名“EngineProperties.plist”,但您的评论认为它正在保存“Data.plist”。你正在加载什么文件名?另外,保存文件时,请记录'plistPath'。如果您在模拟器中运行应用程序,则可以在查找器中查找路径并查看创建的文件,然后在XCode中打开它们以查看其内容。 – Dondragmer 2012-04-11 04:35:26
opps评论是错误的..我更新了一个更具描述性的名字。然而,有一个问题是查找器的路径在哪里?我以前没有听说过这个? – 2012-04-11 20:41:33