I can create an NSDictionary
var namesDictionary=Dictionary<String,String>()
namesDictionary["Jacob"] = "Marry"
But, when I create a empty dictionary like coded below, line 1 i okie, but line 2 (adding values) throws an error.
var namesDictionary =[:]
namesDictionary["Jacob"] = "Marry"
Error is "Cannot assign to the result of this expression". Is there any other way to assign the values.
It looks like it's an issue with swift interpreting the type of your dictionary. Try explicitly typing your empty dictionary.
var namesDictionary: Dictionary<String, String> = [:]
namesDictionary["Jacob"] = "Marry"
I think a better use for [:]
is for emptying an already defined dictionary. If you add a third line namesDictionary = [:]
, you will be able to call namesDictionary["Jacob"] = "Marry"
again since the compiler knows what type of dictionary it is from the inital declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With