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

下面的代码给Sending 'ViewController *const __strong' to parameter of incompatible type 'id<AVAudioPlayerDelegate>' (它是下面的代码中的第三行)的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]; 

如何解决? 我在ViewControllerviewDidLoad的实例方法中有这样的代码。 还有一个类似的问题,那就是一个类方法: 从Class中分配'id <AVAudioPlayerDelegate>'的指针types不兼容

AVAudioPlayerDelegate头文件中的AVAudioPlayerDelegate协议,警告将消失。 通过不声明一个类符合给定的协议,编译器不能保证(至less是警告)你没有实现所需要的方法。 下面的代码是一个纠正的版本,将抑制警告。

 @interface ViewController : UIViewController <AVAudioPlayerDelegate> @end 

有趣的是,这并没有为我做。 我有在头文件中定义的委托 – 在UITableViewController中使用UIImagePickerControllerDelegate 。 由于setDelegate方法需要一个id,所以我将delegate设置为如下所示:

 [myImagePicker setDelegate: (id)self]; 

但我想知道这里发生了什么? 由于这通常不在其他代码示例中显示在其他代码领域。 谁能解释这个?

谢谢

你的自己(你的class级)没有

 @interface MyClassName: NSObject <AVAudioPlayerDelegate> 

在头文件中。

这是告诉你,你错过了在.h文件中定义你的委托

 @interface ClassName : NSObject <YourDeletegate> 

确保你在UIComponent花括号内添加委托

例如:@interface DemoView:UIView <UITableViewDelegate>

而不是委托你正在创build的财产。

@property(retain)id <UITableViewDelegate> demoDelegate