objective c - NCSFDictionary, Mutating method sent to immutable object -
i have started jump realm of objective-c , getting all. have been working on unarchiving file nsmutablearray , initializing in model array. array filled various nsmutabledicationary's. have seen add dictionaries non-mutable, went ahead , copied regular , put them in mutable , remove old one. solution seems work every instance except first.
i @ loss why work first.
here how initializing all
-(id) initwithlist:(nsmutablearray *)savedlist { self = [super init]; if (self) { int size=0; serverlist=[[nsmutablearray alloc] initwitharray:savedlist copyitems:yes]; size=[serverlist count]; for(int i=0;i<size;i++) { loginlist=[nsmutabledictionary dictionarywithdictionary:[serverlist objectatindex:i]]; [serverlist addobject:loginlist]; [serverlist removeobjectatindex:i]; } } return self; }
here code throwing error, value being read off of checkbox in tableview , passed here change value.
-(void)setmount:(int)row value:(nsnumber*)boolasnumber { [[serverlist objectatindex:row] setobject:boolasnumber forkey:@"mountshare"]; }
here error shows when try , change first element
2010-12-01 13:38:54.445 network share[35992:a0f] *** -[nscfdictionary setobject:forkey:]: mutating method sent immutable object
thanks help. if there better way please let me know.
this loop code wrong:
size=[serverlist count]; for(int i=0;i<size;i++) { loginlist=[nsmutabledictionary dictionarywithdictionary:[serverlist objectatindex:i]]; [serverlist addobject:loginlist]; [serverlist removeobjectatindex:i]; }
when remove object, array renumbered. after you've processed 1st object @ index 0, original 2nd object becoming 1st object @ index 0, i
set index 1, original 3rd object is! means you're processing alternate items original array, , 2nd, 4th, etc items never swapped, , that's why errors you're seeing.
one way solve replace "i" in objectatindex:
, removeobjectatindex:
calls "0", you're taking items off front of array.
the alternate solution create separate newserverlist
array , insert new objects that. @ end of loop, release old serverlist , set variable point newserverlist.
Comments
Post a Comment