组合 NSArray
我有 3 个 NSArray,其中:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
将它们“组合”为一个后,我需要:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
我是否首先将所有数组组合为一个,然后求和并删除重复项?
I have 3 NSArrays with:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
After "combining" these into one, I need:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
Do I first combine all the arrays into one and then sum and remove the duplicates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
NSCountedSet
。我不清楚数组中数据的结构,但假设您的B: 2
意味着数组中有两个 B,那么这样的事情就可以工作:You could use an
NSCountedSet
. I'm not clear on the structure of the data in your arrays, but by assuming that yourB: 2
means that you have two B's in the array, then something like this would work:您可以尝试使用 NSMutableDictionary,而不是使用 NSArray,其中键是对象结构中固有的。这将允许您迭代每个字母和计数数组,然后使用键查询值,获取值并添加到值中,然后继续处理。
Instead of using a NSArray you could try using a NSMutableDictionary where the key is inherent in the objects structure. That will allow you to iterate through each of your arrays of letters and counts then query for the value with the key, get the value and add to the value, then continue processing.
一种可能性是使用:
可能需要探讨性能方面的考虑因素。或者,迭代数组,在单独的 NSDictionary 中提取匹配的项目(在项目上键入)并随时求和。
One possibility would be to use:
There may be performance considerations to explore. Alternatively, iterate the array, pulling out matching items in a separate NSDictionary (keyed on item) and summing as you go.