2012-07-14 60 views
0

我刚开始使用Xcode,我正在和一些朋友一起制作一个简单的音板应用程序。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”中以绿色返回“line”,出现SIGABRT错误?

+1

请给我们一个http://www.sscce.org。否则,我几乎可以确保没有人会愿意阅读所有这些。把它分解成更小的片断。有时候,这甚至可以帮助你自己找到答案。 – Drise 2012-07-14 04:04:45

+0

它应该是#import“ViewController.h”而不是“#import”ViewController.h“'。 – Priyal 2015-08-26 13:57:03

回答

4

我有第一线

当然你是问题 - 您已在接口部分有完整的功能。界面应该只包含方法原型。方法定义进入实现部分。您有:

@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 

+0

嘿所有的错误,现在走了,但在运行时,当我点击一个按钮,它关闭,这里需要我 #进口 #进口“AppDelegate.h” INT主(int argc,char * argv []) { @autoreleasepool { return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class])); } } 它突出显示“main.m”中的“return”行,并且出现SIGABRT错误,为绿色? – 2012-07-14 04:30:39

+0

寻找控制台上的错误,并将其粘贴到这里 – tGilani 2012-07-14 04:36:07

+0

这是一个不同的问题 - 可能最好问另一个问题。听起来就像你的代码抛出一个异常,但没有更多的信息很难说。 – Caleb 2012-07-14 05:41:23

1

你不应该方法的定义写在@interface ... @end。你只能声明方法。在@implementation [email protected]中写下你方法的定义,并在@[email protected]中声明你的方法。

我认为这会对您有所帮助。