开发者

"May not respond to" warning

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 {

    NSComp开发者_开发百科arisonResult 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?


In some cases, it can't be defined in classes in that case you can use the following:

[obj performSelector:@selector(methodName)];

Not preferred but when you need it, you need it! :)


Make sure that this method is declared in your @interface!!


That warning usually indicates that you have forgotten to declare the method in the interface of the referred-to object:

@interface BuildingsViewController : ControllerSuperclass {
       // Declaration of ivars...
}

- (NSComparisonResult) searchBuildingObject:(NSDictionary *) building forString:(NSString *) searchString;
// Other method declarations...

@end

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 @interface declaration 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜