i getting error when creating dictionary in swift:
value of type 'string' not conform expected dictionary value type 'anyobject'
code:
let joesmith : [string : anyobject] = ["name" : "joe smith", "height" : 42, "soccer expo" : true, "guardian" : "jim , jan smith"]
swift 3
first of string in swift struct , not conform anyobject.
solution #1
the best solution in swift 3 changing type of dictionary value anyobject any (which includes string struct).
let joesmith : [string : any] = ["name" : "joe smith", "height" : 42, "soccer expo" : true, "guardian" : "jim , jan smith"] solution #2
however if want keep value fo dictionary defined anyobject can force bridge string struct nsstring class adding as anyobject shown below (i did same other values)
let joesmith : [string : anyobject] = [ "name" : "joe smith" anyobject, "height" : 42 anyobject, "soccer expo" : true anyobject, "guardian" : "jim , jan smith" anyobject] swift 2
the problem here defined value of dictionary anyobject , string in swift not object, it's struct.
the compiler complaining string because first error if remove it, give error 42 again int struct.
and have same problem true (bool -> struct).
you can solve problem in 2 ways:
foundation #1
if add import foundation swift struct automatically bridged nsstring (which object) , compiler happy
any #2
you replace anyobject any. can put kind of value in dictionary.
considerations
imho (swift developers) should progressively stop relying on objective-c bridging , use second solution.





Comments
Post a Comment