我有一些NSDictionary
对象存储在被NSArray
调用的telephoneArray
. 我获取键的值,number
然后用NSDictionary
数组中相同索引处的新对象替换我刚刚读取的值。然后我想将这些新对象放入NSSet
. 如何做到这一点?请参阅下面我不成功的尝试。
// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
[newTelephone setBranch:newBranch];
[newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
NSLog(@"%@",[[telephoneArray objectAtIndex:i] valueForKey:@"number"]);
[telephoneArray replaceObjectAtIndex:i withObject:newTelephone];
NSLog(@"phone number %i = %@",i,[[telephoneArray objectAtIndex:i] valueForKey:@"number"]);
}
NSSet *telephoneSet = [NSSet setWithArray:telephoneArray];
NSLog(@"telephoneArray=%i",[telephoneArray count]);
NSLog(@"telephoneSet=%i",[[telephoneSet allObjects] count]);
输出:
2010-03-06 03:06:02.824 AIB[5160:6507] 063 81207
2010-03-06 03:06:02.824 AIB[5160:6507] phone number 0 = 063 81207
2010-03-06 03:06:02.825 AIB[5160:6507] 063 81624
2010-03-06 03:06:02.825 AIB[5160:6507] phone number 1 = 063 81624
2010-03-06 03:06:02.825 AIB[5160:6507] 063 81714
2010-03-06 03:06:02.826 AIB[5160:6507] phone number 2 = 063 81714
2010-03-06 03:06:02.826 AIB[5160:6507] 063 81715
2010-03-06 03:06:02.826 AIB[5160:6507] phone number 3 = 063 81715
2010-03-06 03:06:02.826 AIB[5160:6507] telephoneArray=4
2010-03-06 03:06:02.827 AIB[5160:6507] telephoneSet=1
使用上面的代码,telephoneArray 的计数可以在 1 到 5 之间,但 phoneSet 的值始终为 1。我认为有一个明显的错误,但我看不出在哪里。