-3
該代碼假設聲音是yes或yes 3播放時單擊,但我得到一個錯誤,然後我開始調試,說rand不能靜態分配,但我不能把一個*因爲它是一個整數。當我這樣做時,出現轉換錯誤。我也嘗試在聲音之前放置一個*,並解決了錯誤問題,但是當按鈕被點擊時它不會播放任何東西。那麼我該怎麼做?指針循環到整數轉換?
#import "ViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController()
@end
@implementation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}//this is where the error breakpoint is that you show me how to make
- (IBAction)Yes {
NSInteger rand = arc4random_uniform(2);
NSString sound = rand == 0 ? @"yes" : @"yes 3";
NSURL *url = [[NSBundle mainBundle] URLForResource:sound withExtension:@"mp3"];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
}
@end