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

发布于 2024-08-25 04:45:08 字数 748 浏览 3 评论 0原文

NSMutableArray *insideArray = [[NSMutableArray alloc] init]; NSMutableArray *outsideArray = [[NSMutableArray alloc] init]; [insideArray addObject:@"你好1"]; [insideArray addObject:@"你好2"]; [outsideArray addObject:insideArray]; [insideArray 删除所有对象]; [insideArray addObject:@"你好3"]; [insideArray addObject:@"你好4"]; [outsideArray addObject:insideArray]; 当前的输出是

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

我需要一种方法来获得输出

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

有人有解决方案或者可以看到我哪里出了问题吗?谢谢

NSMutableArray *insideArray = [[NSMutableArray alloc] init];
NSMutableArray *outsideArray = [[NSMutableArray alloc] init];
[insideArray addObject:@"Hello 1"];
[insideArray addObject:@"Hello 2"];
[outsideArray addObject:insideArray];
[insideArray removeAllObjects];
[insideArray addObject:@"Hello 3"];
[insideArray addObject:@"Hello 4"];
[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

べ繥欢鉨o。 2024-09-01 04:45:19

从内存管理的角度来思考。每次您键入“insideArray”时,计算机都会看到对单个内存点的引用。而“[outsideArray addObject:insideArray];”确实将对 insideArray 对象的引用放置在 OutsideArray 中,但 insideArray 仍然指向内存中的同一位置。

尝试实例化一个新对象并将其放入 OutsideArray 中。改变:
[outsideArray addObject:insideArray];

阅读:
[outsideArray addObject:[NSMutableArray arrayWithArray: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. While "[outsideArray addObject:insideArray];" does place a reference to the insideArray object in outsideArray, insideArray still points to the same place in memory.

Try instantiating a new object and placing that in outsideArray. Change:
[outsideArray addObject:insideArray];

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

That should be the simplest solution.

惜醉颜 2024-09-01 04:45:17

好吧,您正在从 insideArray 中删除“Hello 1”和“Hello 2”,这是 insideArray 所保存的实际对象。换句话说,outsideArray 不知道 insideArray 的内容。

Well, you're deleting the "Hello 1" and "Hello 2" from the insideArray which is the actual object held by outsideArray. In other words, the outsideArray has no knowledge of the contents of insideArray.

温柔戏命师 2024-09-01 04:45:16

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

I think that the problems is that when you add an object into an array, the array does not copy the given object, it just holds a reference to it and increments the retain count by one

粉红×色少女 2024-09-01 04:45:15

您对两个子数组使用相同的 NSMutableArray。您需要使用两个不同的对象。具体来说,而不是

[insideArray removeAllObjects];

使用

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

最快的解决方案。

You're using the same NSMutableArray for both sub-arrays. You need to use two different objects. Specifically, instead of

[insideArray removeAllObjects];

instead use

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

for the quickest solution.

御守 2024-09-01 04:45:14

您实际上只是在这里创建两个数组。您正在创建“外部”数组,然后向其中添加对同一内部数组的两个引用。当你输出它时,你会看到这两个内部数组引用完全相同的东西。

您需要在此处创建多个内部数组,因为您希望它们保存不同的值集。

这里的关键点是数组只是对象引用,清空和重新填充数组不会创建新数组;它只是改变你已经拥有的那个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文