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": "john@doe.com"
]
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" = "john@doe.com";
}
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
with UserDefaults
.
There is a dedicated API dictionary(forKey
which returns [String:Any]?
by default. In your case conditionally downcast the dictionary to [String:String]
.
if let sessionData = preferences.dictionary(forKey: "session") as? [String:String] {
print(sessionData["user_id"]!)
} else {
print("sessionData is not available")
}
And this is not JavaScript. Please name variables lowerCamelCased.
A more sophisticated way is to use a struct and save it with Codable
struct Session : Codable {
let userID, name, email : String
}
let preferences = UserDefaults.standard
let sessionInfo = Session(userID:"123", name: "John", email: "john@doe.com")
let sessionData = try! JSONEncoder().encode(sessionInfo)
preferences.set(sessionData, forKey: "session")
do {
if let sessionData = preferences.data(forKey: "session") {
let sessionInfo = try JSONDecoder().decode(Session.self, from: sessionData)
print(sessionInfo.userID)
}
} catch { print(error) }