Objective-C 扩展在某些情况下生成 "may not respond warning"

标签 objective-c cocoa ios

代码生成一个警告并生成预期的输出“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/

相关文章:

ios - 对自定义对象数组内的数字进行排序

ios - 从另一个函数以编程方式启用按钮

iphone - Xcode 构建脚本来分离图像 channel ?

objective-c - 如何在NSMutableArray中交换值(而非索引)

使用 XCTest 和单独文件进行 iOS 测试

ios - 从 Tap Gesture 中获取特定的 Collection View 单元格

objective-c - 子类化一个类,同时需要使用委托(delegate)

macos - vListBox 或 HtmlListbox 的不连续多项选择在 OS X 上的 wxWidgets/wxPython 中不起作用

iphone - 禁用 UIAlertView 按钮会让我的应用程序被拒绝吗?

objective-c - 创建一个没有 Nib 的自定义 NSViewController