ThiS
ThiS

Reputation: 977

Swift dictionary wrong access

I'm trying to get the email value from an Author struct stored in a dictionary. Here some code and you can see that if I'm trying to get it from the dictionary it is not working. Any idea why?

struct Author{
    var name: String
    var email: String
}

var dict_author = [String: Author]()

var aut1 = Author(name: "Author1", email: "[email protected]")

dict_author["Author1"] = aut1 

var a = dict_author["Author1"]

println(a.email) //not working
println(aut1.email) // [email protected]

Upvotes: 1

Views: 92

Answers (1)

Antonio
Antonio

Reputation: 72760

A dictionary lookup always returns an optional, so you have to unwrap it before using:

println(a?.email) 

Suggested reading: Optionals and Dictionaries

Upvotes: 6

Related Questions