指针循环到整数转换?

时间:2014-09-22 00:02:05

标签: objective-c

代码假设在点击时发出声音是“是”或“是”3播放,但在开始调试之前我得到一个错误,说兰特不能静态分配,但是我不能把*放在那里因为它是一个整数。我这样做时收到转换错误。我也试过在声音之前放一个*,这解决了错误问题,但是当点击按钮时它不会播放任何东西。那我该怎么办?

#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

1 个答案:

答案 0 :(得分:0)

之前我已经多次看到过这个问题,这与你的音频播放器变量在有机会播放文件之前发布的事实有关。

您需要在标题中为音频播放器创建一个属性:

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

然后在您的方法中访问该属性而不是本地变量:

- (IBAction)YES {
    NSInteger rand = arc4random_uniform(2);
    NSString *sound = rand == 0 ? @"yes" : @"yes 3";
    NSURL *url = [[NSBundle mainBundle] URLForResource:sound withExtension:@"mp3"];

    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [self.audioPlayer play];
}

请注意,这与你正在搞乱的声音变量完全无关。在创建该变量时,您应该使用*来指示指针,如上所示。