iPhone:NSMutableDictionary 的关键问题重复

发布于 2024-12-28 17:34:26 字数 1237 浏览 1 评论 0原文

我从服务器获取 XML 格式数据并在场景中解析它。我在使用 NSMutableDictionary 将此数据放入键值对时遇到问题。因为,NSMutableDictionary 键是唯一的。因此,如果存在来自 xml 的相同键,它将用先前存在的值覆盖该键。 例如:我的 XML 数据如下。

<?xml version='1.0' encoding='utf-8'?><order>
            <number>123</number>
            <detail>

                <name>shoe</name> 
                <description>This is from nike</description>
                <price>10.00</price>

                <name>discount</name> 
                <description>This is from Arrow</description>
                <price>-1.00</price>

                <name>bag</name> 
                <description>This is a leather one</description>
                <price>10.00</price>

            </detail>
          </order>

[appDelegate.finalOrderDict setObject:trimmString forKey:elementName];

我尝试使用 NSMutableDictionary 将数据放入键值对中,因为我将在另一个视图中显示输出,如下所示。

Summary:
Shoe     $ 10.00
Discount $ -1.00   
Bag      & 10.00
Discount $ -2.00 

但是,由于密钥重复相同,它会覆盖现有密钥中的数据。

如果数据中存在相同的键并且我们尝试将其放入 NSDictionary 中,有人可以帮助我解决键的重复问题吗?

谢谢你!

I am getting an XML format data from server and parsing it in an scenario. I am having problem putting this data in key, value pair using NSMutableDictionary. Because, NSMutableDictionary key is unique. So, it overwrites the key with the previous value existing if there are same keys are coming from xml.
For ex: My XML data is below.

<?xml version='1.0' encoding='utf-8'?><order>
            <number>123</number>
            <detail>

                <name>shoe</name> 
                <description>This is from nike</description>
                <price>10.00</price>

                <name>discount</name> 
                <description>This is from Arrow</description>
                <price>-1.00</price>

                <name>bag</name> 
                <description>This is a leather one</description>
                <price>10.00</price>

            </detail>
          </order>

[appDelegate.finalOrderDict setObject:trimmString forKey:elementName];

I am trying to put the data in key, value pair using NSMutableDictionary, because, i'll be showing the output in another view like below.

Summary:
Shoe     $ 10.00
Discount $ -1.00   
Bag      & 10.00
Discount $ -2.00 

But, as the keys are repeating same, its overwriting the data from the existing one.

Could someone help me resolving duplicating of key in case if there are same keys present in the data and we try to put that in NSDictionary.

Thank you!

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

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

发布评论

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

评论(1

温柔嚣张 2025-01-04 17:34:26

NSDictionary 的行为符合预期 - 当调用 -setObject:forKey: 时,如果旧键和新键对 -isEqual: 响应 YES,则现有键值对将被覆盖>。听起来您需要修改您的数据结构。

一种想法是让 appDelegate.finalOrderDict 成为 NSDictionary 对象的 NSArray。每个 NSDictionary 都会有“名称”和“价格”键值对。

如果每个值已经存在,您也可以将其提升为数组:

id existingValue = [appDelegate.finalOrderDict objectForKey:forKey:elementName];
if ([existingValue isKindOfClass:[NSMutableArray class]]) {
    [existingValue addObject:trimmString];
} else if (existingValue) {
    [appDelegate.finalOrderDict setObject:[NSMutableArray arrayWithObjects:existingValue, trimmedString, nil] forKey:elementName];
} else {
    [appDelegate.finalOrderDict setObject:trimmedString forKey:elementName];
}

但是,这种方法风险更大,因为您必须对从 FinalOrderDict 获取的所有对象进行类型检查,以查看它们是字符串还是数组。

NSDictionary is behaving as intended - when -setObject:forKey: is called, an existing key-value pair is overwritten if the old key and the new key respond YES to -isEqual:. It sounds like you need to modify your data structure.

One idea would be having appDelegate.finalOrderDict instead be an NSArray of NSDictionary objects. Each NSDictionary would then have "name" and "price" key-value pairs.

You could also have each value promoted to an array if it already exists:

id existingValue = [appDelegate.finalOrderDict objectForKey:forKey:elementName];
if ([existingValue isKindOfClass:[NSMutableArray class]]) {
    [existingValue addObject:trimmString];
} else if (existingValue) {
    [appDelegate.finalOrderDict setObject:[NSMutableArray arrayWithObjects:existingValue, trimmedString, nil] forKey:elementName];
} else {
    [appDelegate.finalOrderDict setObject:trimmedString forKey:elementName];
}

This approach is riskier, however, as you have to type-check all objects obtained from finalOrderDict, to see if they are strings or arrays.

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