简体   繁体   中英

How can I convert a 3d immutable array into a mutable array?

It seems that when I use mutableCopy whilst the first dimension of arrays stays mutable, but all the arrays within the array have become immutable.

My current code seems to have no effect, and attempting to edit the array results in an NSInvalidArgumentException.

The code is supposed to make a deep copy of the input (a 3d array) then make sure that it is mutable.

-(NSMutableArray*) copyMutable :(NSMutableArray*) input{
NSMutableArray* copyInput = [[input mutableCopy] autorelease];
NSMutableArray* copy = 
[[[NSMutableArray alloc] initWithArray:copyInput   copyItems:YES] autorelease];
NSMutableArray* copylayer;
NSMutableArray* copylayertwo;

arraycount=0;
arraycountTwo=0;
arraycountThree=0;
NSMutableArray* inputCopy = [[[NSMutableArray alloc] init]autorelease];
for (int a=0; a<[inputCopy count]; a++){
    copylayer=[[[copy objectAtIndex:arraycount] mutableCopy]autorelease];
    [copy replaceObjectAtIndex:arraycount withObject:copylayer];
     for (int b=0; b<[inputCopy count]; b++){
         copylayertwo = [[copy objectAtIndex:arraycount] objectAtIndex:arraycountTwo];
         [[copy objectAtIndex:arraycount] replaceObjectAtIndex:arraycountTwo withObject:copylayertwo];

         arraycountTwo++;
     }
    arraycount++;
}
return copy;

}

Sorry My bad... It seems that i missed out a bit of code. It should be copylayertwo = [[[copy objectAtIndex:arraycount] objectAtIndex:arraycountTwo] mutableCopy] autorelease;

Also it seems that i put in the wrong variable for the loop lengths and to reset the counters.

I should stop staying up late...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM