ios - a faster way to mutate an Array of NSMutableDictionaries -
based on fact cannot edit mutable collections while enumerating them, best solution come edit array of nsmutabledictionaries:
__block nsmutabledictionary *tempdict = [nsmutabledictionary dictionarywithcapacity:1]; __block nsuinteger idx; [_myarray enumerateobjectsusingblock:^(nsmutabledictionary* obj, nsuinteger indx, bool *stop) { if (// condition met) { tempdict = [obj mutablecopy]; idx = indx; } }]; [tempdict setobject:[nsnumber numberwithint:thisqueryresults] forkey:@"resultsnum"]; [_myarray replaceobjectatindex:idx withobject:rowselected]; this seems way complicated (even language obj-c).. , since it's involving 2 data types (nsmutablearray , nsmutabledictionary), doesn't seem can cleanly put them category.. advice?
update: 1 comment asked why create mutablecopy (as opposed copy.. since it's copying mutable object)..
suppose used copy.. if put break on tempdict get:
// tempdict = [obj copy] po tempdict $0 = 0x0b28cc10 <__nsarrayi 0xb28cc10>( 1 ) // tempdict = [obj mutablecopy] po tempdict $0 = 0x0b28cc10 <__nsarraym 0xb28cc10>( //notice m in __nsarraym opposed above 1 ) in case of copy.. if follow line this: [tempdict setobject:[nsnumber numberwithint:thisqueryresults] forkey:@"resultsnum"];
i error:
[__nsdictionaryi setobject:forkey:]: unrecognized selector sent instance 0xb245100 i same above error this code:
for (nsuinteger idx = 0; idx < [_mymutablearray count]; idx++) { nsmutabledictionary* mymutabledict = _mymutablearray[idx]; [mymutabledict setobject:obj forkey:key]; } update 2: origin of problem instantiating non mutable arrays , dictionaries.. i'm new whole new obj-c literals, didn't know create nsmutablearray , nsdictionary, gotta this, respectively:
[@[..] mutablecopy] [@{..} mutablecopy]
so in case, don't quite follow why call tempdict = [obj mutablecopy]; when conditions write dictionary writable.
you can use several tricks. using
for (nsuinteger idx = 0; idx < _myarray.count: idx++_ { nsmutabledictionary *obj = _myarray[idx]; // modify } for nsdictionaries can allkeys , iterate on copy. bit slower using fast enumeration, still faster doing workarounds boxing integers replace later :)
Comments
Post a Comment