开发者

Objective-C extensions generates "may not respond warning" in some cases

The code builds with a single warning and generates the expected output "Hello, World!"

Example file "TestClass.h"

#import <Cocoa/Cocoa.h>
@interface TestClass : NSObject {
}

- (void)foobar;
@end

Example file "TestClass.m"

#import "TestClass.h"

@implementation TestClass
- (void)say {
    // Compiler output "warning: 'TestClass' may not respond to '-hello'"
    [self hello]; 
}

- (void)hello {
    NSLog(@"Hello, World!");
}

- (void)foobar {
    [self say];
}
@end

@interface TestClass ()
- (void)say;
- (void)hello;
@end

The compiler w开发者_开发百科arning can be avoided by putting the "hello" method above "say" in the @implementation section. But it's annoying to be depended on the order you put your methods. Is there any way around this compiler warning, without having to put your methods in any specific order?


No, there isn't. Period.

The compiler parses your code top-down, so please, just put your private @interface defintion above your @implementation and you'll be all good to go.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜