NSMutableDictionary 的 setObject:forKey: 覆盖字典中的所有数据

发布于 2024-12-29 02:49:57 字数 2755 浏览 1 评论 0原文

for ( int cnt = 0 ; cnt < nPeople ; cnt++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, cnt);

    NSString    *firstName  = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    NSString    *lastName   = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
    NSString    *fullName;   

    /* skipped code at here : code to merge firstName and lastName to fullName. In my country, many of us don't separate first name and last name */

    // tempKeyString : NSString variable that has key of fullNameArray value for nameDictionary.
    // fullNameArray : to keep some fullName variables for tempKeyString.
    if (!tempKeyString) // there's no tempKeyString, a.k.a. it's the first fullName.
    {
        // it's not important to know about GetUTF8String:fullName. It's for my own language.
        tempKeyString = [self GetUTF8String:fullName];
        [fullNameArray addObject:fullName];
    }
    else
    {
        if ([tempKeyString characterAtIndex:0] == [[self GetUTF8String:fullName] characterAtIndex:0]) // if fullName has the same tempKey with fullNameArray.
        {
            [fullNameArray addObject:fullName];
        }
        else // if fullName has different tempKey with fullNameArray.
        {
            //tempKey : key data for fullNameArray
            NSString    *tempKey    = [tempKeyString substringToIndex:1];
            // tempDict : to keep the deep copy of nameDictionary before adding new key.
            NSDictionary *tempDict   = [nameDictionary mutableDeepCopy];
            // add new key (tempKey) with new value (fullNameArray)
            // PROBLEM : ALL values (including previous values) in dictionary(nameDictionary) are overwritten to a new value(fullNameArray).
            [nameDictionary setObject:fullNameArray forKey:tempKey];

            //empties fullNameArray so that it can get the new fullName of the new tempKey.
            [fullNameArray removeAllObjects];
            //refresh tempKeyString, and add the new fullName.
            tempKeyString = [self GetUTF8String:fullName];
            [fullNameArray addObject:fullName];
            ...
        }
    }
}

我正在尝试从 iPhone 的联系人中创建一个 NSMutableDictionary 对象。为什么我创建一个 NSMutableDictionary 类型对象是因为我需要联系人的索引,并且直接从 ABAddressRef 类型对象创建索引看起来并不容易。我还需要实现搜索功能..

刚编码时没有问题,但调试后唯一的问题让我抓狂。当我将名为 fullNameArray 且键名为 tempKey 的数组应用到namedDictionary 后,我可以发现 nameDictionary 包含 fullNameArray 的所有值。 所有以前的数据都被覆盖! 在应用 fullNameArray 之前,我尝试制作以前 nameDictionary 的深度复制版本,并将其复制到新的 nameDictionary。但是,当我检查第三行的断点时,我在tempDict中找不到以前的数据。

我添加了更多代码和注释。它可能比我的解释更有帮助......任何问题都很高兴!

我整夜试图从这里 - StackOverflow - 和其他网页找到原因,但我找不到任何类似的问题..请帮助我!提前非常感谢!

for ( int cnt = 0 ; cnt < nPeople ; cnt++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, cnt);

    NSString    *firstName  = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    NSString    *lastName   = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
    NSString    *fullName;   

    /* skipped code at here : code to merge firstName and lastName to fullName. In my country, many of us don't separate first name and last name */

    // tempKeyString : NSString variable that has key of fullNameArray value for nameDictionary.
    // fullNameArray : to keep some fullName variables for tempKeyString.
    if (!tempKeyString) // there's no tempKeyString, a.k.a. it's the first fullName.
    {
        // it's not important to know about GetUTF8String:fullName. It's for my own language.
        tempKeyString = [self GetUTF8String:fullName];
        [fullNameArray addObject:fullName];
    }
    else
    {
        if ([tempKeyString characterAtIndex:0] == [[self GetUTF8String:fullName] characterAtIndex:0]) // if fullName has the same tempKey with fullNameArray.
        {
            [fullNameArray addObject:fullName];
        }
        else // if fullName has different tempKey with fullNameArray.
        {
            //tempKey : key data for fullNameArray
            NSString    *tempKey    = [tempKeyString substringToIndex:1];
            // tempDict : to keep the deep copy of nameDictionary before adding new key.
            NSDictionary *tempDict   = [nameDictionary mutableDeepCopy];
            // add new key (tempKey) with new value (fullNameArray)
            // PROBLEM : ALL values (including previous values) in dictionary(nameDictionary) are overwritten to a new value(fullNameArray).
            [nameDictionary setObject:fullNameArray forKey:tempKey];

            //empties fullNameArray so that it can get the new fullName of the new tempKey.
            [fullNameArray removeAllObjects];
            //refresh tempKeyString, and add the new fullName.
            tempKeyString = [self GetUTF8String:fullName];
            [fullNameArray addObject:fullName];
            ...
        }
    }
}

I'm trying to make a NSMutableDictionary object from the contacts of my iPhone. Why I make a NSMutableDictionary typed object is that I need indexes for contacts, and it doesn't look easy to make indexes from ABAddressRef typed object directly. I also need to make searching function..

There was no problem when I just coded, but after debugging the only problem makes me crazy. After I apply the array named fullNameArray with the key named tempKey to the namedDictionary, I can find the nameDictionary has all values with them of fullNameArray. All previous data were overwritten! I tried to make a deep copied version of previous nameDictionary before applying fullNameArray and copy it to the newer nameDictionary. However, when I checked the breakpoint at the third line, I can't find the previous data at the tempDict.

I added more codes and comments. It might help more than my explanation.. any questions are pleased!

I tried to find the reason from here - StackOverflow -, and other webpages all the night, but I couldn't find any similar problems.. please help me! Thank you so much in advance!!

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

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

发布评论

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

评论(1

烟柳画桥 2025-01-05 02:49:57

它被清空的原因是因为

[nameDictionary setObject:fullNameArray forKey:tempKey];

在这里,您使用对象“fullNameArray”设置了字典,然后

[fullNameArray removeAllObjects];

删除了该数组中的所有值,有效地删除了“nameDictionary”中的对象,它们是同一个对象,而不是您存储在字典中的 fullNameArray 的深层副本。无论如何,为什么您需要将任何内容存储到数组中?您只存储 1 个值。

[nameDictionary setObject:fullName forKey:tempKey];

会做你需要的。抱歉,如果我问错了你的问题,这很难理解

The reason why it get emptied is because

[nameDictionary setObject:fullNameArray forKey:tempKey];

here, you set up your dictionary with the object "fullNameArray", then

[fullNameArray removeAllObjects];

remove all the values inside this array, effectively, removing your object in the "nameDictionary", they are the same object, it's not a deep copy of fullNameArray that you store inside your dictionary. Why did you need to store anything into your array anyway? You're only storing 1 value.

[nameDictionary setObject:fullName forKey:tempKey];

will do what you need. Sorry if I mistaken your question, it's quite hard to understand

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