-1
所以我一直在爲我的移動應用程序開發類做一個應用程序,我需要找到一個解決方案來解決我在保存名爲「events」的全局數組時遇到的問題。如何在applicationWillTerminate中保存全局數組並將其重新加載到應用程序函數中?
在這裏,我試圖重新加載在AppDelegate中保存的事件類,但它並沒有在主屏顯示控制器改變:當有人退出應用程序
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Override point for customization after application launch.
//Load the events that were saved when application was terminated
let eventKey = NSUserDefaults.standardUserDefaults()
Global.events = (eventKey.arrayForKey("savedEvents") as? [Event])!
return true
}
這裏是調用的函數:
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
let myEvents = Global.events
NSUserDefaults.standardUserDefaults().setObject(myEvents, forKey: "savedEvents")
NSUserDefaults.standardUserDefaults().synchronize()
}
這可能只是顯示此數組的視圖控制器中的錯誤,但如果您看到代碼有問題,請讓我知道。