So I basically have a login screen on my app and after a successful login, before performing a segue to the main screen I save the following info in a Dictionary in session. It's something like this (putting static info for the example):
let preferences = UserDefaults.standard
let session_info : [String: String] = [
"user_id": "123",
"name": "John",
"email": "[email protected]"
]
preferences.set(session_info, forKey: "session")
preferences.synchronize()
So the session data gets saved, if I assign it to a variable and print it, I get an optional dictionary of info in my console.
let session_data = preferences.value(forKey: "session")!
If I do a print(session_data)
I get this output:
{
"user_id" = "123";
"name" = "John";
"email" = "[email protected]";
}
My issue is that I am not sure how to access the keys, because if I treat the assigned variable as a Dictionary it says Type 'Any' has no subscript members.
What I use is this:
print(session_data["user_id"])
Is there a way to access this info or do I have to save each variable to a separate session key?
Basically never use
value(forKey
withUserDefaults
.There is a dedicated API
dictionary(forKey
which returns[String:Any]?
by default. In your case conditionally downcast the dictionary to[String:String]
.And this is not JavaScript. Please name variables lowerCamelCased.
A more sophisticated way is to use a struct and save it with
Codable