我想保存在名爲MyPos.h類使用NSCoding方法的一些數據:如何實現applicationWillTerminate在appDelegate.m
(void)encodeWithCoder:(NSCoder *)aCoder
(id)initWithCoder:(NSCoder *)aDecoder
這些MyPos.m文件中已經實現了兩種方法。
現在我希望應用程序在數據完成啓動時終止並加載數據時保存數據。我想在AppDelegate.m文件中實現to方法,但是我無法理解如何保存包含數據的MyPos類的實例。
這是AppDelegate.m文件中的兩種方法之一...到目前爲止:
- (void)applicationWillTerminate:(UIApplication *)application {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject: **HELP_HERE!** forKey:@"myPos"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];}
我怎樣才能解決這個問題? 非常感謝。
您的應用程序委託中是否有MyPos類的實例?保存實例... – jmstone617 2012-04-09 16:15:48
''applicationWillTerminate''有點無用。你最好在''applicationWillEnterBackground''中實現你的寫程序,或者「儘快」。從iOS 4開始,當按住home時,您的應用程序將不會收到「applicationWillTerminate」。如果您的應用程序在後臺,並且由於內存不足導致您的應用程序被終止,您將不會收到「applicationWillTerminate」。 – 2012-04-09 16:21:31
我有三個班。 AppDelegate,MyPos和一個ViewController。 ViewController創建一個MyPos實例來保存數據。 AppDelegate還創建一個MyPos實例來保存對象。但是,如果我在applicationDidEnterBackground的末尾添加一個NSLog來檢查MyPos實例的屬性,它們都會顯示爲空。我有感覺,我正在處理兩個不同的對象 - 或者至少有一個缺失的鏈接。有什麼想法嗎?謝謝一堆。 – gotfunk 2012-04-09 19:23:26