ios - Collection <__NSArrayM: 0x7fa1f2711910> was mutated while being enumerated -
there number of questions similar title none them of helped me.
but can relate solution of 'nsgenericexception', reason: collection <__nsarraym: 0x7fabb400> mutated while being enumerated question question. because termination happening when i'm adding,removing string object array.
error :
terminating app due uncaught exception 'nsgenericexception', reason: '*** collection <__nsarraym: 0x7fa1f2711910> mutated while being enumerated.'
first throw call stack:
( 0 corefoundation 0x00000001087b8c65 __exceptionpreprocess + 165 1 libobjc.a.dylib 0x0000000108422bb7 objc_exception_throw + 45 2 corefoundation 0x00000001087b85c4 __nsfastenumerationmutationhandler + 132 3 0x0000000107e24cf5 -[professionalregsecond tableview:diddeselectrowatindexpath:] + 773 4 uikit 0x0000000109759d71 -[uitableview _deselectrowatindexpath:animated:notifydelegate:] + 347 5 uikit 0x000000010974deea -[uitableview _userselectrowatpendingselectionindexpath:] + 187 6 uikit 0x000000010968062c _applyblocktocfarraycopiedtostack + 314 7 uikit 0x00000001096804a6 _aftercacommithandler + 533 8 corefoundation 0x00000001086ebca7 __cfrunloop_is_calling_out_to_an_observer_callback_function__ + 23 9 corefoundation 0x00000001086ebc00 __cfrunloopdoobservers + 368 10 corefoundation 0x00000001086e1a33 __cfrunlooprun + 1123 11 corefoundation 0x00000001086e1366 cfrunlooprunspecific + 470 12 graphicsservices 0x000000010d6c8a3e gseventrunmodal + 161 13 uikit 0x000000010965c900 uiapplicationmain + 1282 14 0x0000000107de14cf main + 111 15 libdyld.dylib 0x000000010b585145 start + 1 )
libc++abi.dylib: terminating uncaught exception of type nsexception
what i'm trying :
i need array in such way when select cell want add object array & when i'm deselecting row want remove row array result array selected entries. how ?
here code
@implementation professional { nsmutablearray *updatedlocalityarray; // alloc,init did in viewdidload nsstring *tocheck; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *tableviewcell = [tableview cellforrowatindexpath:indexpath]; nslog(@"%@",tableviewcell.textlabel.text); tocheck = tableviewcell.textlabel.text; [updatedlocalityarray addobject:tocheck]; nslog(@"%@ *****", updatedlocalityarray); } - (void)tableview:(uitableview *)tableview diddeselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *tableviewcell = [tableview cellforrowatindexpath:indexpath]; nslog(@"%@",tableviewcell.textlabel.text); (nsstring *test in updatedlocalityarray) { if ([test isequaltostring:tableviewcell.textlabel.text]) { [updatedlocalityarray removeobject:test]; nslog(@"%@ *****", updatedlocalityarray); } } }
you cannot delete items nsmutablearray
while iterating it.
there several solutions this:
- iterate copy of array
or
- use index-based
for
loop instead offor each
syntax.
not copying array saves allocation , few cpu cycles:
for (int = updatedlocalityarray.count-1 ; >= 0 ; i--) { nsstring *test = updatedlocalityarray[i]; if ([test isequaltostring:tableviewcell.textlabel.text]) { [updatedlocalityarray removeobjectatindex:i]; nslog(@"%@ *****", updatedlocalityarray); } }
Comments
Post a Comment