nsmutablearray - Issue in replacing value in NSMutable Array in swift -
i have nsmutable array in swift contains inner array respective key this.
array sample: { item = ( studio, "1 bed", "2 bed", "3 bed", "4 bed", "5+ bed", "individual lease" ); status = ( none, none, none, none, none, none, none ); }, { item = ( "1+ bath", "2+ bath", "3+ bath", "4+ bath" ); status = ( none, none, none, none ); }, { item = ( "on campus", "off campus", "downtowm lafayette", lafayette ); status = ( none, none, none, none ); }
code is:
var itemsarray = nsmutablearray() var itemarr1:[string] = "studio,1 bed,2 bed,3 bed,4 bed,5+ bed,individual lease".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) var statusarr1:[string] = "none,none,none,none,none,none,none".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) var itemarr2:[string] = "1+ bath,2+ bath,3+ bath,4+ bath".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) var statusarr2:[string] = "none,none,none,none".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) var itemarr3:[string] = "on campus,off campus,downtowm lafayette,lafayette".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) var statusarr3:[string] = "none,none,none,none".componentsseparatedbycharactersinset(nscharacterset (charactersinstring: ",")) itemsarray.insertobject(["item": itemarr1, "status": statusarr1], atindex: 0) itemsarray.insertobject(["item": itemarr2, "status": statusarr2], atindex: 1) itemsarray.insertobject(["item": itemarr3, "status": statusarr3], atindex: 2) println("\(itemsarray)") itemsarray.objectatindex(0).valueforkey("item").replaceobjectatindex(0, withobject: "replacevalue")
now when going replace index status or key array value crashing. in short need replace value of index of nested array. same patterns works nice in objective c. if have idea or code related these type please me. admirable. lot in advance.
i suspect problem here way in mixing native swift types objective-c types. if code reflects desired data structure, why not use native swift types? want becomes pretty simple:
var itemsarray = [dictionary<string, [string]>]() var itemarr1: [string] = "studio,1 bed,2 bed,3 bed,4 bed,5+ bed,individual lease".componentsseparatedbystring(",") var statusarr1: [string] = "none,none,none,none,none,none,none".componentsseparatedbystring(",") var itemarr2: [string] = "1+ bath,2+ bath,3+ bath,4+ bath".componentsseparatedbystring(",") var statusarr2: [string] = "none,none,none,none".componentsseparatedbystring(",") var itemarr3: [string] = "on campus,off campus,downtowm lafayette,lafayette".componentsseparatedbystring(",") var statusarr3: [string] = "none,none,none,none".componentsseparatedbystring(",") itemsarray.append(["item": itemarr1, "status": statusarr1]) itemsarray.append(["item": itemarr2, "status": statusarr2]) itemsarray.append(["item": itemarr3, "status": statusarr3]) println("\(itemsarray)") itemsarray[0]["item"]![0] = "replacevalue"
itemsarray
native swift data structure -- array of dictionaries, each dictionary maps string array of strings. change value, assign it.
update
as specific reason code wasn't working, i'll confess find bit murky. let's @ failing line again:
itemsarray.objectatindex(0).valueforkey("item").replaceobjectatindex(0, withobject: "replacevalue")
and here broken down each call, showing type returned each call:
itemsarray // nsmutablearray .objectatindex(0) // anyobject .valueforkey("item") // anyobject .replaceobjectatindex(0, withobject: "replacevalue") // blows
the interesting thing here way swift implicitly downcasting anyobjects
. recall nsarray/nsdictionary/etc have contents un-typed, accessing them returns generic anyobject
can try downcast more specific types.
in first case, swift handling automatically, downcasting anyobject
returned objectatindex
nsdictionary
can call valueforkey
on it.
in second case, however, unwilling downcast anyobject
nsmutablearray
can call replaceobjectatindex
. why difference between these 2 cases?
i'm not sure. seems swift unwilling downcast mutable
flavor of objective-c collections swift counterparts. object supplied in case swift array (an array<string>
precise). though object declared var
, swift refuses allow object go array<string>
anyobject
nsmutablearray
.
you can see pretty trying following:
var test_array = ["hello", "there"] (test_array anyobject) nsarray // works (test_array anyobject) nsmutablearray // blows
for me, moral of story here don't use ns collection types swift unless have to. , when use them in swift code, make sure you know types of objects going in , out of them , careful of swift's being good-natured trying smooth away difference between native swift collection types , objective-c counterparts.
Comments
Post a Comment