我刚刚开始使用Xcode,我和一些朋友正在制作一个简单的音板应用程序。
我遇到了第一行的问题。我已经编写了代码,我很确定它是正确的,但我不明白为什么会这样!我得到了一个“预期的”;'在方法原型之后“并尝试了一切来解决它,只需插入分号就会给我带来3个错误。这是我的代码
'#import "ViewController.h"'
@interface ViewController ()
// Sound for Ben
- (IBAction)playsound1 { // this is where the error is. It is not on any other line
NSString *path = [[NSBundle mainBundle] pathForResource:@"Ben" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Bruno
- (IBAction)playsound2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Bruno" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Homer
- (IBAction)playsound3 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Homer" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Jon
- (IBAction)playsound4 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Jon" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Mark
- (IBAction)playsound5 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Mark" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Mikey
- (IBAction)playsound6 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Mikey" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Omar
- (IBAction)playsound7 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Omar" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Tom
- (IBAction)playsound8 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Tom" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// Sound for Victor
- (IBAction)playsound9 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Victor" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
嘿所有错误现在都消失了,但是当它运行时,当我点击一个按钮时,它会关闭并带我到这里
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
它突出显示绿色的“main.m”中的“return”行,带有SIGABRT错误?
答案 0 :(得分:5)
我遇到第一行的问题
当然你是 - 你的界面部分有完整的功能。界面应仅包括方法原型。方法定义在实现部分中。你有:
@interface ViewController ()
- (IBAction)playsound1 { // this is where the error is. It is not on any other line
NSString *path = [[NSBundle mainBundle] pathForResource:@"Ben" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
应该是:
@interface ViewController ()
- (IBAction)playsound1;
// ...
@end
其次是:
@implementation ViewController
- (IBAction)playsound1 { // this is where the error is. It is not on any other line
NSString *path = [[NSBundle mainBundle] pathForResource:@"Ben" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
// ...
@end
(我添加了一些缩进以便于阅读。)
答案 1 :(得分:1)
您不应该在@interface ... @end
中写出方法的定义。您只能声明方法。在@implementation ....@end
中写下方法的定义,并在@interfaece...@end
中声明您的方法。
我认为这会对你有所帮助。