Dictionary of swift classes with strings -


i have class identity , dictionary of instances of them strings keys. want access 1 of instances string , change of properties. i'm trying use switch statement access instances in dictionary depending on value of string.

class identity {     let provider: string     let uid: string?     let token: string?     let name: string?      init(provider: string){         self.provider = provider         self.uid = nil         self.token = nil         self.name = nil     } }  var identities = [string:identity]()  identities["twitter"] = identity(provider: "twitter")  identities["twitter"].uid = "131241241241"  identities["twitter"].name = "@freedrull"  let provider: string = "twitter"  var i: identity? {     switch provider {     case "twitter":         return identities["twitter"] identity?     case "facebook":         return identities["facebook"] identity?     case "soundcloud":         return identities["soundcloud"] identity?     default:         return nil     } }  if != nil {     i.name = "tony" } 

i error assigning i.name "tony". need cast i identity somehow? thought was.

you have declared i optional:

var i: identity? // ... 

so it's still optional. it's not identity. it's optional wrapping identity. can't do optional - until unwrap it. unwrap it, @ identity. have:

if != nil {     i.name = "tony" } 

instead:

if let = {     i.name = "tony" } 

or:

if != nil {     i!.name = "tony" } 

both ways of unwrapping optional.

or, test , unwrap in 1 move:

i?.name = "tony" 

then you'll have new problem; have declared name constant. can't change constant! have:

let name: string? 

instead:

var name: string? 

[by way, of code redundant:

init(provider: string){     self.provider = provider     self.uid = nil     self.token = nil     self.name = nil } 

uid, token, , name optionals, already nil. can cut 3 lines.]


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -