组合 NSMutableSets 时出现问题
我有以下代码:
NSMutableSet* localSet = [[NSMutableSet alloc] initWithArray:symbols];
NSMutableArray* fetchedSymbolsArray = [NSMutableArray array];
for (NSDictionary* symbol in fetchedSymbols) {
[fetchedSymbolsArray addObject:[NSDictionary dictionaryWithObject:[symbol valueForKey:@"symbol"] forKey:@"symbol"]];
}
NSMutableSet* serverSet = [[NSMutableSet alloc] initWithArray:fetchedSymbolsArray];
[localSet unionSet:serverSet];
for (NSDictionary* symbol in localSet) {
NSLog(@"%@",[symbol valueForKey:@"symbol"]);
}
我希望 serverSet 中的所有内容都位于 localSet 中。这段代码没有反映这一点。
如果不将任何重复项添加到 localSet 中,那也是更好的选择。
编辑: 这是我的日志:
2011-08-16 17:46:28.887 Stream[94612:207] YHOO
2011-08-16 17:46:28.887 Stream[94612:207] GOOG
2011-08-16 17:46:28.887 Stream[94612:207] INTC
2011-08-16 17:46:28.888 Stream[94612:207] BIDU
2011-08-16 17:46:28.888 Stream[94612:207] INTC
2011-08-16 17:46:28.888 Stream[94612:207] BIDU
2011-08-16 17:46:28.888 Stream[94612:207] AAPL
2011-08-16 17:46:28.888 Stream[94612:207] AAPL
2011-08-16 17:46:28.889 Stream[94612:207] AMD
2011-08-16 17:46:28.889 Stream[94612:207] GMCR
I have the following code:
NSMutableSet* localSet = [[NSMutableSet alloc] initWithArray:symbols];
NSMutableArray* fetchedSymbolsArray = [NSMutableArray array];
for (NSDictionary* symbol in fetchedSymbols) {
[fetchedSymbolsArray addObject:[NSDictionary dictionaryWithObject:[symbol valueForKey:@"symbol"] forKey:@"symbol"]];
}
NSMutableSet* serverSet = [[NSMutableSet alloc] initWithArray:fetchedSymbolsArray];
[localSet unionSet:serverSet];
for (NSDictionary* symbol in localSet) {
NSLog(@"%@",[symbol valueForKey:@"symbol"]);
}
I want everything in serverSet to be in localSet. This code does not reflect this.
It would also be preferable if any duplicates were not added to localSet.
EDIT:
Here is my log:
2011-08-16 17:46:28.887 Stream[94612:207] YHOO
2011-08-16 17:46:28.887 Stream[94612:207] GOOG
2011-08-16 17:46:28.887 Stream[94612:207] INTC
2011-08-16 17:46:28.888 Stream[94612:207] BIDU
2011-08-16 17:46:28.888 Stream[94612:207] INTC
2011-08-16 17:46:28.888 Stream[94612:207] BIDU
2011-08-16 17:46:28.888 Stream[94612:207] AAPL
2011-08-16 17:46:28.888 Stream[94612:207] AAPL
2011-08-16 17:46:28.889 Stream[94612:207] AMD
2011-08-16 17:46:28.889 Stream[94612:207] GMCR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
[localSet unionSet:serverSet]
编辑
这是仅使用符号而不是
NSDictionary
的代码:try
[localSet unionSet:serverSet]
EDIT
Here's code that just uses symbols instead of
NSDictionary
s:您的 serverSet 和 localSet 在联合中以错误的方式存在 - 被修改的集合是方法“unionSet:”的接收者,而不是参数。
请注意,根据定义,集合不允许重复的条目。但这并不意味着字典中不会有重复的值,这意味着您不会两次拥有相同的“相等”字典(如果两个字典各自包含相同数量的条目,则它们是相等的,并且,对于给定的键,每个字典中对应的值对象满足 isEqual: 测试)。
为了避免特定字典值的重复,您必须自己添加它们。我建议您创建一个临时集,其中包含“symbol”键的实际值,并使用该集来测试它是否已添加。
而不是
You've got your serverSet and localSet around the wrong way in the union - the set being modified is the receiver of the method "unionSet:", not the argument.
Note that set's, by definition, won't allow duplicate entries. But that doesn't mean that you won't have duplicates of values within the dictionary, it means that you won't have the same "equal" dictionary twice (two dictionaries are equal if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual: test).
To avoid duplicates of a particular dictionary value, you'd have to add them yourself. I'd recommend you create a temporary set which contains the actual values for the "symbol" key and use that set to test whether it's already been added.
Instead of