简体   繁体   中英

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 warning 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM