代码生成一个警告并生成预期的输出“Hello, World!”
示例文件“TestClass.h”
#import <Cocoa/Cocoa.h>
@interface TestClass : NSObject {
}
- (void)foobar;
@end
示例文件“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
通过将“hello”方法放在@implementation 部分的“say”上方,可以避免编译器警告。但是依赖于你放置方法的顺序是很烦人的。有什么方法可以解决这个编译器警告,而不必按任何特定顺序放置您的方法?
最佳答案
不,没有。 期间。
编译器自上而下解析您的代码,所以请将您的私有(private)@interface
定义放在您的@implementation
上,然后您'一切顺利。
关于Objective-C 扩展在某些情况下生成 "may not respond warning",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4569414/