2012-07-14 61 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]中聲明你的方法。

我認爲這會對您有所幫助。