簡體   English   中英

預期的標識符錯誤

[英]Expected identifier error

因此,如果有人可以幫助我,我只會在- (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

方法定義不應在@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];
}

您是否要在頭文件(.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM