如何在Objective-C中创建和调用基本方法?

Tal*_*lon 0 cocoa cocoa-touch objective-c

我正在尝试在目标C中创建一个基本方法(函数)并且遇到一些错误,这里是代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self setupWebView];
}

- (void)setupWebView {
    NSLog(@"Testing"); 

}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

接收器类型'Reading'为实例消息不声明带有选择器'setupWebView'的方法

知道我做错了什么吗?

Jul*_*ien 5

只有在看到为源代码行上方的类声明了方法时,编译器才会同意接收器响应的内容.因此@interface,要在类的声明中声明方法,在类的某些类声明或实现中声明方法.

如果您不想-(void)setupWebView在公共场所导出@interface,那么简单的方法是@interface在您的.m文件中添加您的类扩展:

// additional internal methods for my class
@interface MyClass ()

- (void)setupWebView;

@end

@implementation MyClass

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self setupWebView];
}

- (void)setupWebView {
    NSLog(@"Testing"); 

}

@end
Run Code Online (Sandbox Code Playgroud)