我有一个排序数组,包含一系列演示文稿中所有扬声器的名称。我粘贴了下面的代码,但是我的具体问题完全在第二个for循环中。有多个演示者在MyClass.speeches中拥有多个条目。因此,我第一次在'for'循环中敲入'if'语句,并且它的计算结果为true,我看到日志语句“添加了一个语句...”。然而,第二次它应该评估为真(通过'if'正上方的日志语句验证),我从来没有看到在日志上打印的“添加语音...”行。那么为什么我在第一次“item.speaker”和“obj”是一样的时候没有进入'if'呢?字符串比较只匹配第一次
[speakers sortUsingSelector:@selector(compare:)];
for (id obj in speakers) {
//now create a unique list of speakers
if ([uniqP containsObject:obj]) {
NSLog(@"already have this object");
}
else {
[uniqP addObject:obj];
//now that we've found a new speaker, we need to go through the entire list of speeches and populate
//them in the correct order.
self.speechesByPresenter = [[NSMutableArray alloc] init];
for (int j=0; j<numEntries; j++) {
SpeechesItem *item = [MyClass.speeches objectAtIndex:j];
NSLog(@" %@--%@--%d (%@)", item.speaker, obj, j, item.title);
if(item.speaker == obj) {
[self.speechesByPresenter addObject:item];
NSLog(@"added a speech, %@ now has %d", item.speaker, [self.speechesByPresenter count]);
}
}
}
}
扬声器对象是什么类?如果它是一个NSString,那么@dasblinkenlight具有正确的代码。如果它是一个自定义类,那么你需要实现'isEqual'和'hash'函数,并使用'isEqual'而不是== ==。 – lnafziger 2012-08-06 03:48:17