1

我刚开始使用 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 错误?

4

2 回答 2

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

(为了便于阅读,我添加了一些缩进。)

于 2012-07-14T04:06:44.263 回答
1

您不应该在@interface ... @end. 您只能声明方法。将方法的定义写入@implementation ....@end并在@interfaece...@end.

我想这会对你有所帮助。

于 2012-07-14T04:17:22.413 回答