如何解决警告:将'ViewController*const __strong'发送到不兼容类型'id <AVAudioPlayerDelegate>的参数?

Jer*_*y L 35 iphone avfoundation ios

以下代码给出了警告Sending 'ViewController *const __strong' to parameter of incompatible type 'id<AVAudioPlayerDelegate>'(它是以下代码中的第三行):

NSURL *sound0URL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"0" ofType:@"aiff"]];

audioPlayer0 = [[AVAudioPlayer alloc] initWithContentsOfURL:sound0URL error:nil];

[audioPlayer0 setDelegate: self];   // <---   this line causes the warning
[audioPlayer0 prepareToPlay];

audioPlayer0.currentTime = 0;
[audioPlayer0 play];
Run Code Online (Sandbox Code Playgroud)

怎么修好?我在这个ViewController实例方法中有这个代码viewDidLoad.还有一个类似的问题但是这是一个类方法:从'Class'分配给'id <AVAudioPlayerDelegate>'的指针类型不兼容

Cod*_*aFi 77

符合AVAudioPlayerDelegate头文件中的协议,警告将消失.通过不声明类符合给定的协议,编译器无法保证(至少警告)您未能实现它所需的方法.以下代码是一个将禁止警告的更正版本.

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@end
Run Code Online (Sandbox Code Playgroud)


Raj*_*ain 31

有趣的是,这并没有为我做.我在头文件中定义了委托 - 在UITableViewController中使用UIImagePickerControllerDelegate.由于setDelegate方法需要一个id,我将代理设置如下:

[myImagePicker setDelegate: (id)self];
Run Code Online (Sandbox Code Playgroud)

但我想知道这里发生了什么?因为这通常不会出现在其他代码区域的其他委托示例中.谁能解释清楚这个?

谢谢


Poc*_*chi 5

你的自我(你的班级)没有

@interface MyClassName: NSObject <AVAudioPlayerDelegate>
Run Code Online (Sandbox Code Playgroud)

在头文件中。