Swift UserDefaults в AppDelegate.Swift не работает (NSMutableDictionary) - PullRequest
0 голосов
/ 21 сентября 2019

Редактировать # 3

Идея в том, что я хочу сохранить данные loginUata currentUser через глобальную переменную, существующую в AppDelegate, но проблема возникла, когда я попытался установить данные UserDefaults.standard.set (currentUser,forKey: 'currentUser') к концу я получил это исключение (https://justpaste.it/4tj02).

AppDelegate:

var currentUser : [String:Any]?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunching launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // loading current user
    if let user = UserDefaults.standard.dictionary(forKey: "currentUser"), user["id"] != nil  {

        currentUser = user
        print("AppDelegate => currentUser : ", user)

        // accessing TabBar controller via Main.storyboard
        let TabBar = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBar")

        // assigning TabBar as RootViewController of the project
        window?.rootViewController = TabBar
    }

    return true
}

Логин:

// fetching all JSON info received from the server
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary

// save mode of casting JSON
guard let parsedJSON = json else {
  return
}
// saving logged user
currentUser = parsedJSON
if UserDefaults.standard.dictionary(forKey: "currentUser") != nil {
   print("Exist !!")
} else {
   print("Add it")
   UserDefaults.standard.set(currentUser, forKey: "currentUser")
   helper.instanceViewController(identifier: "HomeID", animated: true, by: self, completion: nil)

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...