预期的标识符错误

时间:2014-06-25 12:03:22

标签: ios objective-c identifier

所以我只会在- (IBAction)playAudio:(id)sender下出现一个错误,如果有人可以提供帮助,我会感激的是,当我按下按钮时我试图只播放声音。

#import "SecondViewController.h"

@interface SecondViewController ()

- (IBAction)playAudio:(id)sender {
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
    NSURL *audioPath = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];
}

@end

@implementation SecondViewController{

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)ButtonPressed:(UIButton *)sender forEvent:(UIEvent *)event {
}

- (IBAction)Button {AudioServicesPlaySystemSound(soundID);
}

- (IBAction)playAudio:(id)sender {
}

@end

2 个答案:

答案 0 :(得分:0)

方法定义不应该在@interface部分;只是声明

@interface SecondViewController ()

- (IBAction)playAudio:(id)sender;

@end

然后将定义移到@implementation部分,并进行以下更改:

您正在为不同的对象使用标识符(变量名称)audioPath两次。将第二个更改为audioURL或某些内容:

- (IBAction)playAudio:(id)sender {
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];   // HERE
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];
}

答案 1 :(得分:0)

你是否在头文件(.h)中实现 - (IBAction)playAudio:(id)sender {} ???

您可以在.h中声明playAudio IBAction,但在.m中实现它

您的文件应如下所示:

@interface SecondViewController ()

- (IBAction)playAudio:(id)sender;

@end

@implementation SecondViewController{

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)ButtonPressed:(UIButton *)sender forEvent:(UIEvent *)event {
}

- (IBAction)Button {AudioServicesPlaySystemSound(soundID);
}

- (IBAction)playAudio:(id)sender {
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
    NSURL *audioPath = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];
}

@end