我总是在各种不同的代码项目中遇到这个错误,我就是不知道错误是什么。我使用的是Xcode 6和Objective C语言。错误为:
预期;方法原型之后。
#import "ViewController.h"
@interface ViewController ()
//the error is on the next line
-(IBAction)number1:(id)sender{
SelectNumber = * 10;
}
不能将方法体放在@interface
块中。您只能将它们放在@implementation
块中。
Xcode的UIViewController
子类模板将类扩展名(@interface ViewController () ... @end
)和实现块(@implementation ViewController ... @end
)都放在.m
文件中。您意外地在类扩展中定义了方法。您需要将其向下移动到实现块。你最终应该得到这样的东西:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)number1:(id)sender{
SelectNumber *= 10;
}
@end
您可以在.h和/或.m文件的"@interface
"部分中定义原型。
这些应该只是以分号结尾的一行方法声明。
您应该将整个功能实现转移到"@implementation
"部分,这就是您在上面所做的。只需将"@interface
"更改为"@implentation
",就可以编译了。
如果您希望您的函数公开给其他类&对象,将方法声明放在.h文件中。