Sunday 15 January 2012

swift - Merging Dictionaries that are -



swift - Merging Dictionaries that are <String, Any> -

i trying merge 2 dictionaries, both of type:

var dict = dictionary<string, any>

i have been using code merge them:

extension dictionary { mutating func extend(with:dictionary){ (key,value) in { self.updatevalue(value, forkey:key) } } } ... dict.extend(["num":123,"str":"abc","obj":uicolor.bluecolor()])

this lastly line produces error:

cannot convert expression's type '(with: dictionary<string, any>)' type 'dictionary<string, nsobject>'

so ive been trying solve changing declaration of extend to:

mutating func extend(with:dictionary<string, any>

but results in line self.updatevalue in producing error:

'protocol<>' not convertible 'value'

my best guess here .updatevalue objective c method, , objective c struggles any info type.

so next move seek , convert <string, any> object <string, nsobject> object in hope .updatevalue take it. when seek this:

var dict2 = dictionary<string, nsobject>

xcode wants set semi colon after nsobject. if complains else. ive seen before, seems xcode can tell not right, cant set finger on it.

so issue this:

i have 2 dictionaries. they both have <string, any>. i have merge them one.

anyone have idea's on how this?

the problem here:

dict.extend(with:["num":123,"str":"abc","obj":uicolor.bluecolor()]) ^^^^^

the first parameter of class/struct methods has no automatic external name, have phone call as:

dict.extend(["num":123,"str":"abc","obj":uicolor.bluecolor()])

if want utilize external name, have explicitly declare it:

mutating func extend(withdict with:dictionary) { ^^^^^^^^

or if want utilize same local name:

mutating func extend(# with:dictionary){ ^

suggested reading: external parameter names

this code used testing:

var dict = dictionary<string, any>() extension dictionary { mutating func extend(with:dictionary){ (key,value) in { self.updatevalue(value, forkey:key) } } } dict.extend(["num":123,"str":"abc","obj":uicolor.bluecolor()])

swift

No comments:

Post a Comment