let dict1 : [String:Any] = ["id":19,"userinfo": ["name":"janak",
"mobileN":999889,
"email": "[email protected]"]]
how i gat email value from user info ? my code is
let cvc = dict1["userinfo"]
print(cvc!)
CodePudding user response:
You need to tell the compiler what type you expect to get when you query userinfo
. Since the expectation can fail, you then also need to unwrap the optional (the ?
in my example) and what you expect to get when reading email
:
let email: String? = (dict1["userinfo"] as? [String:Any])?["email"] as? String
CodePudding user response:
you need to typecast key to a particular type, since user info is again dictionary,we need to typecast it .
if let userInfoData = dict1["userinfo"] as? [String: Any], let email = userInfoData["email"] as? String {
print("Email: \(email)") }
CodePudding user response:
you can use optional chaining to access the email value:
if let email = (dict1["userinfo"] as? [String:Any])?["email"] as? String {
print(email)
}