【问题标题】:Forward declaration of objective-c class extension methodObjective-C类扩展方法的前向声明
【发布时间】:2015-03-20 01:04:43
【问题描述】:

以下代码产生错误No visible @interface for 'Bar' 在-[Foo fooMethod] 的第二行实现中声明选择器'barMethod'

//  FooBar.m

#import "FooBar.h"

//////////////////////////////////

@implementation Foo

- (void)fooMethod {
    Bar *bar = [Bar new];
    [bar barMethod]; // Error: No visible @interface for 'Bar' declares the selector 'barMethod'
}

@end

//////////////////////////////////

@interface Bar ()
- (void)barMethod;
@end

@implementation Bar

- (void)barMethod {
    // do something
}

@end

除了将Bar 类扩展移到Foo 实现之上(有时不是很方便)之外,还有什么方法可以在FooBar.m 中转发声明-[Bar barMethod]

【问题讨论】:

标签: objective-c class-extensions


【解决方案1】:

为了方法可见性,扩展的接口与任何其他接口一样:编译器必须在使用之前查看声明。*不幸的是,您必须将@interface 放入标题中或在文件中比Foo 的实现更靠前。


*我知道的一个例外是那些根本没有在接口中命名的方法——本质上是由它们的定义声明的——并且在同一个@implementation块中使用。无论顺序如何,编译器都会为您解决。

【讨论】:

    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多