A very specific question, so doing research on it is kind of hard. It seems pretty straightforward to me, but I’m doing something wrong and I can’t see what it is.
I’ve created the following method:
- (NSComparisonResult) searchBuildingObject:(NSDictionary *) building forString:(NSString *) searchString {
NSComparisonResult buildingComparison = [[building objectForKey:@"building"] compare:searchString
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchString length])];
if (buildingComparison != NSOrderedSame) {
for (NSString *alias in [building objectForKey:@"alias"]) {
NSComparisonResult aliasComparison = [alias compare:searchString
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchString length])];
if (aliasComparison == NSOrderedSame)
return aliasComparison;
}
}
return buildingComparison;
}
It seems to be working fine, even when I test the method it gives me the correct results. Yet, I still get a warning upon calling the method:
[self searchBuildingObject:[self.buildings objectForKey:building] forString:searchText]
I made sure building is an NSDictionary, and searchString is definitely an NSString. The result of the method is an NSComparisonResult which I then compare to NSOrderedSame
NSComparisonResult result = [self searchBuildingObject:[self.buildings objectForKey:building] forString:searchText];
if (result == NSOrderedSame) NSLog(@"Same");
The warning I’m getting is “BuildingsViewController may not respond to ‘-searchBuildingObject:forString:’. Does anybody see what’s wrong?
That warning usually indicates that you have forgotten to declare the method in the interface of the referred-to object:
The reason it’s a warning and not an error is that it’s the compiler complaining that it’s not certain that you can call that method on that object. The
@interfacedeclaration is what the compiler looks at to make that determination.The compiler also knows about methods which were not declared, but which are defined before you call them (this only applies within a single compilation unit — loosely speaking, a class can call its own method which doesn’t have a declaration, without the compiler complaining, if the method’s implementation is located previous to its use in the file; not a great thing to rely on).
The method lookup/resolution is done later, at runtime, and the compiler doesn’t know about that. Because the method does actually exist on your class, the call succeeds at that time.