2016-03-08 24 views
-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() 
} 

這可能只是顯示此數組的視圖控制器中的錯誤,但如果您看到代碼有問題,請讓我知道。

回答

0

兩個問題:

  • 在iOS中,有沒有這樣的事情「有人退出應用程序」,這樣的話你在做什麼是不大可能奏效,因爲applicationWillTerminate不會被調用。當應用程序停用或背景時保存。

  • 事件數組不能神奇地保存在用戶默認值中,因爲事件不是屬性列表類型。您需要使Event可存檔並存檔陣列。

相關問題