Swift dictionary bug? -


so started project in swift, , i've come problem:

this code works:

var dictionary = ["a":"valueofa","b":"valueofb","c":"valueofc"] println(dictionary) dictionary["c"] = "newvalofc" println(dictionary) 

and doesn't:

var dictionary = [:] dictionary = ["a":"valueofa","b":"valueofb","c":"valueofc"] println(dictionary) dictionary["c"] = "newvalofc" println(dictionary) 

gives error:

playground execution failed: error: <repl>:35:17: error: cannot assign result of expression dictionary["c"] = "newvalc" ~~~~~~~~~~~~~~~ ^ 

notice not constant value

so why doesn't line

dictionary = ["a":"valueofa","b":"valueofb","c":"valueofc"] 

give error?

since context not provide enough information infer type, you'll need explicitly name dictionary, otherwise swift assumes nsdictionary (i'm not clear on why though. assume better obj-c compatibility):

the following code works:

// playground import uikit  var str:nsstring = "hello, playground"  var d0 = [:] var d1: dictionary = [:]  d0.setvalue(uiwebview(), forkey: "asdf")  d1["asdf"] = 1 d1["qwer"] = "qwer" 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -