我有以下代码:
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
答案 0 :(得分:6)
尝试[localSet unionSet:serverSet]
修改强>
这里的代码只使用符号代替NSDictionary
s:
NSArray *symbols = [NSArray arrayWithObjects:@"AAPL",@"GOOG",@"INTC",@"YHOO",nil];
NSArray *fetchedSymbols = [NSArray arrayWithObjects:@"AMD",@"BIDU",@"GOOG",@"GMCR",@"INTC",@"YHOO",nil];
NSMutableSet* localSet = [[NSMutableSet alloc] initWithArray:symbols];
NSMutableSet* serverSet = [[NSMutableSet alloc] initWithArray:fetchedSymbols];
[localSet unionSet:serverSet];
for (id symbol in localSet) {
NSLog(@"%@",symbol);
}
2011-08-16 18:25:22.107 so7086790[39810:a0f] YHOO
2011-08-16 18:25:22.116 so7086790[39810:a0f] AMD
2011-08-16 18:25:22.116 so7086790[39810:a0f] AAPL
2011-08-16 18:25:22.116 so7086790[39810:a0f] INTC
2011-08-16 18:25:22.117 so7086790[39810:a0f] GMCR
2011-08-16 18:25:22.117 so7086790[39810:a0f] GOOG
2011-08-16 18:25:22.118 so7086790[39810:a0f] BIDU
答案 1 :(得分:0)
你在union中错误地使用了serverSet和localSet - 被修改的集合是方法“unionSet:”的接收者,而不是参数。
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"]);
}
请注意,根据定义,set不允许重复输入。但这并不意味着你不会在字典中有重复的值,这意味着你不会有两次相同的“相等”字典(如果它们各自拥有相同数量的条目,则两个字典是相同的,对于给定的键,每个字典中的相应值对象满足isEqual:test)。
为避免重复特定字典值,您必须自己添加它们。我建议你创建一个临时集,其中包含“symbol”键的实际值,并使用该集来测试它是否已被添加。
NSMutableSet *localSetValues = [[NSMutableSet alloc] init];
// Add local set values
for (NSDictionary *symbol in localSet) {
[localSetValues addObject:[symbol valueForKey:@"symbol"]];
}
// Add server set, conditionally
for (NSDictionary *symbol in serverSet) {
if (![localSetValues containsObject:[symbol valueForKey:@"symbol"]]) {
[localSet addObject:symbol];
}
}
// Cleanup
[localSetValues release];
而不是
[localSet unionSet:serverSet];