重新启动应用程序Swift 4时,UserDefaults有时不保留已保存的值

时间:2018-08-18 00:52:22

标签: ios swift nsuserdefaults

我正在保存情节提要名称和当前的viewController,以便当用户在主屏幕上单击“恢复”时(在HomeScreenVC中为resumeButtonWasPressed),可以将其立即返回到先前查看的视图。每次在班级中设置值后,每次打印这些值时,都会打印出正确的值,例如“ Chapter1”,“ Page2”。但是,当用户在主屏幕上按“恢复”按钮时,只有百分之一的时间可以进入正确的视图。要保存的正确值会在类本身中打印出来,但是当我从HomeScreenVC中的UserDefaults检索值时,有时会打印出错误的故事板和viewController。我的代码在下面。

Cancel

以下是我的HomeScreenVC代码,其中删除了不必要的代码:

npm audit fix --force

这是我的infoScreen VC,其中删除了不必要的内容:

+-----------+  +-----------+  +---------------+  +-------------------+
|   users   |  |   roles   |  |  tradeunions  |  |  user_role_trade  |
+-----------+  +-----------+  +---------------+  +-------------------+
|   id      |  |   id      |  |    id         |  |     user_id       |
|   name    |  |   name    |  |    name       |  |     role_id       |
+-----------+  +-----------+  +---------------+  |     trade_id      |
                                                 +-------------------+

这是我的page1VC,其中删除了不必要的内容:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    _ = UserDefaults.standard
    _ = ["strength" : 0]
    _ = ["intellect" : 0]
    _ = ["charisma" : 0]
    _ = ["name" : ""]
    _ = ["storyBoard" : "Main"]
    _ = ["viewController" : "HomeScreenVC"]

    // Override point for customization after application launch.
    return true
}

这是我的Page2VC,其中删除了不必要的内容:

class HomeScreenVC: UIViewController {
    let defaults = UserDefaults.standard

    @IBAction func resumeButtonWasPressed(_ sender: Any) {
        let sb = defaults.string(forKey: "storyBoard")
        let vc = defaults.string(forKey: "viewController")
        let storyBoard : UIStoryboard = UIStoryboard(name: sb!, bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: vc!)
        self.navigationController?.pushViewController(newViewController, animated: true)
    }

    override func viewDidAppear(_ animated: Bool) {
        let sb = defaults.string(forKey: "storyBoard")
        let vc = defaults.string(forKey: "viewController")
        print(sb!)
        print(vc!)
    }
}

2 个答案:

答案 0 :(得分:0)

尝试以此替换applicationDidFinishLaunchingWithOptions内的代码:

let defaults = UserDefaults.standard
defaults.set(0, forKey: "strength")
defaults.set(0, forKey: "intellect")
defaults.set(0, forKey: "charisma")
defaults.set("", forKey: "name")
defaults.set("Main", forKey: "storyBoard")
defaults.set("HomeScreenVC", forKey: "viewController")

// Override point for customization after application launch.
return true

编辑

如果您已经设置了UserDefaults键,还应该签入applicationDidFinishLaunchingWithOptions,如果此时尚未设置UserDefaults键,则仅在其中进行设置。例如:

let defaults = UserDefaults.standard
if defaults.object(forKey: "storyBoard") == nil { // you could use any other key that you use in the app
    defaults.set(0, forKey: "strength")
    defaults.set(0, forKey: "intellect")
    defaults.set(0, forKey: "charisma")
    defaults.set("", forKey: "name")
    defaults.set("Main", forKey: "storyBoard")
    defaults.set("HomeScreenVC", forKey: "viewController")
}

否则,它们会在每次应用启动时被覆盖。

答案 1 :(得分:-1)

在用户默认设置中设置了所有有效值后,您就缺少synchronize()。只需在设置新值后添加即可:

UserDefaults.standard.synchronize()