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
Post a Comment