如何在应用程序中保存数据

时间:2016-06-09 17:35:16

标签: ios swift

我是初学编程,并且有一点Java经验,如果你们能帮助我解决我遇到的问题,我会非常感激。目前我正在尝试在关闭应用程序时保存数组,并在应用程序再次加载后将其打印到日志中,但无论何时关闭它我都会收到错误

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

以下是我尝试保存代码的部分代码。它位于appdelegate文件中,我认为这是放置它的正确位置。

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let data = NSUserDefaults.standardUserDefaults()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return true
    }
    var savedAssignments = ""
    func applicationWillResignActive(application: UIApplication) {
                      data.setValue(Assignments, forKey: "savedAssignments")
    }

    func applicationDidEnterBackground(application: UIApplication) {
               data.setValue(Assignments, forKey: "savedAssignments")
    }

    func applicationWillEnterForeground(application: UIApplication) {
         }

    func applicationDidBecomeActive(application: UIApplication) {
        print(data)
        print(savedAssignments)
        print(Assignments)
    }
    func applicationWillTerminate(application: UIApplication) {
        }
    }

0 个答案:

没有答案