简体   繁体   English

Objective-C-如何在2D NSMutableArrays中添加对象

[英]Objective-C - How to add objects in 2d NSMutableArrays

NSMutableArray *insideArray = [[NSMutableArray alloc] init]; NSMutableArray * insideArray = [[NSMutableArray alloc] init]; NSMutableArray *outsideArray = [[NSMutableArray alloc] init]; NSMutableArray * outsideArray = [[NSMutableArray alloc] init]; [insideArray addObject:@"Hello 1"]; [insideArray addObject:@“ Hello 1”]; [insideArray addObject:@"Hello 2"]; [insideArray addObject:@“ Hello 2”]; [outsideArray addObject:insideArray]; [outsideArray addObject:insideArray]; [insideArray removeAllObjects]; [insideArray removeAllObjects]; [insideArray addObject:@"Hello 3"]; [insideArray addObject:@“ Hello 3”]; [insideArray addObject:@"Hello 4"]; [insideArray addObject:@“ Hello 4”]; [outsideArray addObject:insideArray]; [outsideArray addObject:insideArray]; The current output is 当前输出是

    array(
      (
       "Hello 3",
       "Hello 4"
       ),
      (
       "Hello 3",
       "Hello 4"
       )
      )

I need a way to get the output to be 我需要一种方法来获得输出

    array(
      (
       "Hello 1",
       "Hello 2"
       ),
      (
       "Hello 3",
       "Hello 4"
       )
      )

Does anyone have a solution or could see where i've gone wrong? 有没有人有解决方案,或者可以看到我哪里出问题了? Thanks 谢谢

You're only actually creating two arrays here. 您实际上只是在这里创建两个数组。 You're creating the "outside" array, and then adding two references to the same inner array to it. 您要创建“外部”数组,然后将两个引用添加到同一内部数组。 When you output it, you're seeing those two inner arrays referencing exactly the same thing. 输出时,您会看到这两个内部数组引用的是完全相同的东西。

You need to create more than one inner array here, since you expect them to hold different sets of values. 您需要在此处创建多个内部数组,因为您希望它们保存不同的值集。

The key point here is that the arrays are just object references, and emptying and refilling the array doesn't create a new one; 这里的关键点是数组只是对象引用,清空和重新填充数组不会创建一个新的数组。 it just changes the one you've already got. 它只会更改您已经拥有的一个。

You're using the same NSMutableArray for both sub-arrays. 您正在为两个子数组使用相同的NSMutableArray。 You need to use two different objects. 您需要使用两个不同的对象。 Specifically, instead of 具体来说,代替

[insideArray removeAllObjects];

instead use 代替使用

[insideArray release];
insideArray = [NSMutableArray array];

for the quickest solution. 寻求最快的解决方案。

我认为问题在于,当您将一个对象添加到数组中时,该数组不会复制给定的对象,它仅保留对其的引用,并将保留计数增加一

Well, you're deleting the "Hello 1" and "Hello 2" from the insideArray which is the actual object held by outsideArray. 好吧,您要从insideArray删除“ Hello 1”和“ Hello 2”,这是outsideArray持有的实际对象。 In other words, the outsideArray has no knowledge of the contents of insideArray. 换句话说,outsideArray不了解insideArray的内容。

Think about it from the standpoint of memory management. 从内存管理的角度考虑它。 Each time you type "insideArray" the computer sees a reference a single point of memory. 每次键入“ insideArray”时,计算机都会看到一个引用,指向单个内存点。 While "[outsideArray addObject:insideArray];" 而“ [outsideArray addObject:insideArray];” does place a reference to the insideArray object in outsideArray, insideArray still points to the same place in memory. 确实在outsideArray中放置了对insideArray对象的引用,insideArray仍指向内存中的同一位置。

Try instantiating a new object and placing that in outsideArray. 尝试实例化一个新对象,并将其放置在outsideArray中。 Change: [outsideArray addObject:insideArray]; 更改:[outsideArray addObject:insideArray];

to read: [outsideArray addObject:[NSMutableArray arrayWithArray:insideArray]]; 读取:[outsideArray addObject:[NSMutableArray arrayWithArray:insideArray]];

That should be the simplest solution. 那应该是最简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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