2011-04-23 92 views
0

.M編碼:XCode-需要幫助的錯誤(預期;和預期的聲明)

-(void)viewDidLoad { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"]; 
    self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] 
    autorelease]; //error: expected ';' before 'autorelease' and // error: expected statement before ']' token 
    theAudio.delegate = self; 
    [theAudio play]; 
    theAudio.numberOfLoops = -1; 

    } 

相關的警告:

warning: property 'theAudio' requires method '-theAudio' to be defined - use @synthesize, @dynamic or provide a method implementation

warning: property 'theAudio' requires the method 'setTheAudio:' to be defined - use @synthesize, @dynamic or provide a method implementation

告訴我,如果你需要的.h編碼。但那裏沒有錯誤。

回答

0

兩個錯誤是因爲你缺少中前行的開口[

self.theAudio = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] autorelease]; 

的警告是因爲你在你的@implementation忘記@synthesize theAudio;(或忘記編寫自定義getter和setter方法)。在運行時,如果你不修正這個問題,你會得到一個未知的選擇器異常。

0

這是我在上一個問題,我的代碼是錯誤的,這應該更正:

self.theAudio = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL] autorelease]; 

這個錯誤:

warning: property 'theAudio' requires method '-theAudio' to be defined - use @synthesize, @dynamic or provide a method implementation

你知道一個屬性是什麼?如果不是,請看this short tutorial。我實際上在你之前的問題中提供了代碼,但你必須知道把它放在哪裏。

+0

屬性是使用@屬性關鍵字在'@ interface'塊中聲明的東西。它基本上是一些語法糖,所以你可以使用'object.theAudio'和'object.theAudio = value'這樣的語法,而不是'[object theAudio]'和'[object setTheAudio:value]'。 – Anomie 2011-04-23 15:26:26

+0

是的,非常感謝你們。這真的有幫助 – user722566 2011-04-23 15:37:38