I have a sorted array containing the names of all the speakers from a long series of presentations. I’ve pasted the code below, but my specific problem comes entirely inside the second ‘for’ loop. There are multiple presenters who have more than one entry in MyClass.speeches. So the first time I hit the ‘if’ statement inside the second ‘for’ loop, and it evaluates to true, I see the log statement “added a speech…”. However, the second time it should evaluate to true (as verified by the log statement directly above the ‘if’), I never see the “added a speech…” line printed to the log. So why am I not entering the ‘if’ after the first time that ‘item.speaker’ and ‘obj’ are the same?
[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]);
}
}
}
}
Instead of
item.speaker == objwhich compares pointers, you should use[item.speaker isEqualToString:obj]which compares contents of strings.