【问题标题】:How can I convert a 3d immutable array into a mutable array?如何将 3d 不可变数组转换为可变数组?
【发布时间】:2012-06-13 07:24:06
【问题描述】:

似乎当我使用 mutableCopy 时,数组的第一个维度保持可变,但数组中的所有数组都变得不可变。

我当前的代码似乎没有任何效果,尝试编辑数组会导致 NSInvalidArgumentException。

代码应该对输入(一个 3d 数组)进行深层复制,然后确保它是可变的。

-(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;

}

【问题讨论】:

    标签: objective-c arrays deep-copy


    【解决方案1】:

    对不起,我的错...似乎我错过了一些代码。它应该是 copylayertwo = [[[copy objectAtIndex:arraycount] objectAtIndex:arraycountTwo] mutableCopy] autorelease;

    另外,我似乎为循环长度输入了错误的变量并重置了计数器。

    我应该停止熬夜......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2018-04-24
      • 2021-07-29
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多