Iphone 保留plist但松开plist值
我正在创建一个plist来保存一些值,但是在测试期间,我注意到在应用程序关闭并从多任务中删除后,我保留了我新创建的plist。但是,如果应用程序从多任务处理中删除,而不是关闭应用程序,我会在plist中释放我的值 这是我的plist控制器类中的save data方法,它管理所有的读/写/保存等Iphone 保留plist但松开plist值,iphone,ios,plist,Iphone,Ios,Plist,我正在创建一个plist来保存一些值,但是在测试期间,我注意到在应用程序关闭并从多任务中删除后,我保留了我新创建的plist。但是,如果应用程序从多任务处理中删除,而不是关闭应用程序,我会在plist中释放我的值 这是我的plist控制器类中的save data方法,它管理所有的读/写/保存等 - (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion
- (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
我的问题分为两部分。。
我是否可以保存这些值,以便即使应用程序已从多任务栏中删除,它们仍保留在plist中。
如果它可以工作,我需要更改什么才能使其工作?这可能与调用saveData方法的位置有关。查看应用程序委托,默认情况下,应用程序中有一些存根。您可能希望applicationWillResignActive、ApplicationIdentinterBackground或applicationWillTerminate。检查每个文档,您想要的文档将取决于您希望何时写入数据 可能不是问题,但您可以在字典上调用writeToFile:,它将作为plist输出。您正在保存到文件名
EngineProperties.plist
,但您的注释认为它正在保存数据.plist
。你正在加载什么文件名?此外,保存文件时,请记录plistPath。如果您在模拟器中运行应用程序,您可以转到finder中的路径,查看创建了哪些文件,并在XCode中打开它们以查看其内容。opps注释错误。。我更新了一个更具描述性的名字。然而,一个问题是finder中的路径在哪里?我以前没听说过这个?对,所以你认为我应该在调用其中一个appdelegate方法时调用这个方法。。是否有一种特殊的方法需要调用它(即不创建另一个实例),或者只要我调用此方法,这种方法就无关紧要。问题是,每次我向plist添加新值时,都会调用savedata方法。。在测试时,我确保保存了所有值,并且当应用程序从多任务中删除时,这些值仍然被删除。。。。。。