我正在創建一個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