将 NSArray 添加到 NSDictionary 会导致问题

发布于 2024-09-08 16:26:23 字数 1849 浏览 1 评论 0原文

我正在 XCode 3.2.3 中为 iOS 4 编写一个应用程序。在我的代码中,我正在创建一个 NSDictionaries 的 NSArray,每个包含两个键/值对、一个字符串和一个 NSDictionaries 的 NSArray(我知道有点令人困惑) 。只是为了给您一个视觉效果,这是结构:

<NSArray>
    <NSDictionary>
        <NSString/>
        <NSArray>
            <NSDictionary/>
            <NSDictionary/>
            ...
        </NSArray>
    </NSDictionary>
    ...
</NSArray>

奇怪的是,当我向最里面的 NSArray 添加一个新的 NSDictionary 时,它似乎将相同的字典添加到外部的 each 中包含的 NSArray 中。 NS 词典。以下是我将最里面的 NSDictionary 添加到最里面的 NSArray 的方法:

[[outerDict objectForKey:@"innerArray"] addObject:innerDict];

因此,回顾一下,我想要的输出是这样的:

<outerArray>

    <outerDict1>
        <string>
        <innerArray1>
            <innerDict1>
            <innerDict2>
        </innerArray1>
    </outerDict1>

    <outerDict2>
        <string>
        <innerArray2>
            <innerDict3>
            <innerDict4>
        </innerArray2>
    </outerDict2>

<outerArray>

但我实际得到的是这样的:

<outerArray>

    <outerDict1>
        <string>
        <innerArray1>
            <innerDict1>
            <innerDict2>
            <innerDict3>
            <innerDict4>
        </innerArray1>
    </outerDict1>

    <outerDict2>
        <string>
        <innerArray2>
            <innerDict1>
            <innerDict2>
            <innerDict3>
            <innerDict4>
        </innerArray2>
    </outerDict2>

<outerArray>

请注意,内部 NSDictionaries 被添加到 每个innerArray。对于我的一生,我无法弄清楚为什么!

任何帮助将不胜感激,谢谢!

-马特

I'm writing an app in XCode 3.2.3 for iOS 4. In my code, I am creating an NSArray of NSDictionaries, each containing two key/value pairs, a string and an NSArray of NSDictionaries (kind of confusing, I know). Just to give you a visual, here is the structure:

<NSArray>
    <NSDictionary>
        <NSString/>
        <NSArray>
            <NSDictionary/>
            <NSDictionary/>
            ...
        </NSArray>
    </NSDictionary>
    ...
</NSArray>

What's weird is that when I add a new NSDictionary to the innermost NSArray, it seems to add the same dictionary to the NSArray contained within each of the outer NSDictionaries. Here's how I'm adding the innermost NSDictionary to the innermost NSArray:

[[outerDict objectForKey:@"innerArray"] addObject:innerDict];

So, just to recap, the output I want is something like this:

<outerArray>

    <outerDict1>
        <string>
        <innerArray1>
            <innerDict1>
            <innerDict2>
        </innerArray1>
    </outerDict1>

    <outerDict2>
        <string>
        <innerArray2>
            <innerDict3>
            <innerDict4>
        </innerArray2>
    </outerDict2>

<outerArray>

But what I'm actually getting is this:

<outerArray>

    <outerDict1>
        <string>
        <innerArray1>
            <innerDict1>
            <innerDict2>
            <innerDict3>
            <innerDict4>
        </innerArray1>
    </outerDict1>

    <outerDict2>
        <string>
        <innerArray2>
            <innerDict1>
            <innerDict2>
            <innerDict3>
            <innerDict4>
        </innerArray2>
    </outerDict2>

<outerArray>

Notice that the inner NSDictionaries are added to every innerArray. For the life of me I can't figure out why!!

Any help would be very greatly appreciated, thank you!

-Matt

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

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

发布评论

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

评论(1

别理我 2024-09-15 16:26:23

您需要发布更多代码。

[[outerDict objectForKey:@"innerArray"] addObject:innerDict];

仅将 innerDict 添加到 outerDict 中的 innerArray,没有其他内容。

也就是说,您可能已将相同的 innerArray 两次放入两个不同的 outerDict 中,如下所示:

NSMutableArray* innerArray=[NSMutableArray array];

[outerDict1 setObject:innerArray forKey:@"innerArray"];
[outerDict2 setObject:innerArray forKey:@"innerArray"];

这会添加相同 > innerArray 两次,而不是两个独立的数组。然后,如果修改 outerDict1 中的 innerArray,也会修改 outerDict2 中的 innerArray,因为这两个是相同的数组。

You need to post more code.

[[outerDict objectForKey:@"innerArray"] addObject:innerDict];

adds innerDict only to innerArray in outerDict, nothing else.

That said, you might have put the same innerArray twice into two different outerDict, like this:

NSMutableArray* innerArray=[NSMutableArray array];

[outerDict1 setObject:innerArray forKey:@"innerArray"];
[outerDict2 setObject:innerArray forKey:@"innerArray"];

This adds the same innerArray twice, not two independent arrays. Then, if you modify the innerArray in outerDict1, that will modify the innerArray in outerDict2 too, because these two are the same arrays.

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