ios - How to shift items in a NSMutableArray -


i have nsmutablearray. checking each item in array 1 1 using loop , remove current item. want next index item 1st index. like

array = 1,2,3,4,5,6

array = 3,4,5,6

i remove indexes using loop. how can shift next element removed index position

updated

  (int i=0; i<[m3u8 count]; i++) {        nsstring *str=[nsstring stringwithformat:             @"http://cp2.comci.com:1000/server/default.stream/%@",            [mutarraymp3files objectatindex:i]       ];       [audioplayer queue:str];       [mutarraymp3files removeobjectatindex:i];       // here want make array continous one.      // let's removed object @ index 0, 1st object should move 0th location,      // 2nd should move 1st.... likewise   } 

you cannot modify nsmutablearray while you're in midst of iterating on it. , if shrinking array loop, can't keep incrementing index use or "run off end" eventually. can make copy, , modify that; or, use while loop instead of iterator:

nsmutablearray *array = @{@1, @2, @3, @5, @6}; while([array count]) {     nsnumber firstelement = [array firstobject];     // firstelement;     [array removeobjectatindex:0]; } 

the problem current code end trying reference items in array index out of bounds, because array getting smaller go. here's how tweak code closer op:

// assumes [m3u8 count] equals original size of mutarraymp3files (int i=0; i<[m3u8 count]; i++) {    nsstring *str=[nsstring stringwithformat:         @"http://cp2.comci.com:1000/server/default.stream/%@",        // line have crashed:        // [mutarraymp3files objectatindex:i]        [mutarraymp3files objectatindex:0]   ];   [audioplayer queue:str];   // line have crashed   // [mutarraymp3files removeobjectatindex:i];   // remove object processed, other objects "shift down" 1 index   [mutarraymp3files removeobjectatindex:0]; 

}


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -