XCode-需要帮助解决错误(预期;和预期声明)

时间:2011-04-23 15:20:30

标签: xcode

.m编码:

-(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;

   }

相关警告:

  

警告:属性'theAudio'需要   要定义的方法'-theAudio' - 使用   @synthesize,@ dynamic或提供一个   方法实现

     

警告:属性'theAudio'需要   方法'setTheAudio:'是   定义 - 使用@synthesize,@ dynamic或   提供方法实现

告诉我你是否需要.h编码。但那里没有错误。

2 个答案:

答案 0 :(得分:0)

在上一个问题中,我是我的,我的代码是错误的,这应该纠正它:

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

出现此错误:

  

警告:属性'theAudio'需要   要定义的方法'-theAudio' - 使用   @synthesize,@ dynamic或提供一个   方法实现

你知道房产是什么吗?如果没有,请查看this short tutorial。我实际上在你之前的问题中提供了代码,但是你必须知道把它放在哪里。

答案 1 :(得分:0)

这两个错误是因为您错过了上一行的开头[

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

警告是因为您在@implementation中忘记了@synthesize theAudio;(或忘记编写自定义getter和setter方法)。在运行时,如果不解决这个问题,你将获得一个未知的选择器异常。