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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10377705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:09:39  来源:igfitidea点击:

How to solve the warning: Sending 'ViewController *const __strong' to parameter of incompatible type 'id<AVAudioPlayerDelegate>?

iphoneiosavfoundation

提问by Jeremy L

The following code gave a warning of Sending 'ViewController *const __strong' to parameter of incompatible type 'id<AVAudioPlayerDelegate>'(it is the third line in the following code):

下面的代码给出了一个警告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];

How can it be fixed? I have this code in the ViewController's instance method of viewDidLoad. There was another question similar but that was a class method: Incompatible pointer types assigning to 'id<AVAudioPlayerDelegate>' from 'Class'

如何修复?我在此代码ViewController的的实例方法viewDidLoad。还有另一个类似的问题,但这是一个类方法:不兼容的指针类型分配给 'id<AVAudioPlayerDelegate>' from 'Class'

回答by CodaFi

Conform to the AVAudioPlayerDelegateprotocol in your header file and the warning will go away. By not declaring that a class conforms to a given protocol, the compiler cannot guarantee (well, at least warn) about your failure to implement the methods required of it. The following code is a corrected version that will suppress the warning.

遵守AVAudioPlayerDelegate头文件中的协议,警告就会消失。通过不声明一个类符合给定的协议,编译器不能保证(好吧,至少警告)你没有实现它所需的方法。以下代码是将抑制警告的更正版本。

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@end

回答by Rajive Jain

Interestingly that didn't do it for me. I have the delegate defined in header file - working with UIImagePickerControllerDelegatein a UITableViewController. Since setDelegate method expected an id, I set delegate as follows instead:

有趣的是,这并不适合我。我在头文件中定义了委托 -在UITableViewController 中使用UIImagePickerControllerDelegate。由于 setDelegate 方法需要一个 id,我改为如下设置委托:

[myImagePicker setDelegate: (id)self];

But I would like to know what's happening here ? Since this is not normally showing itself in other delegate examples in other areas of code. Could anyone elucidate on this ?

但我想知道这里发生了什么?因为这通常不会出现在其他代码区域的其他委托示例中。任何人都可以阐明这一点吗?

Thanks

谢谢

回答by Pochi

Your self (your class) doesnt have the

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

@interface MyClassName: NSObject <AVAudioPlayerDelegate>

in the header file.

在头文件中。

回答by Kirit Vaghela

This is a warning telling you that you are missed to define your delegate in .h file

这是一个警告,告诉您您错过了在 .h 文件中定义您的委托

@interface ClassName : NSObject <YourDelegate>

回答by sourabh sharotria

Make sure you adding delegate inside UIComponent braces

确保在 UIComponent 大括号内添加委托

Eg: @interface DemoView : UIView < UITableViewDelegate>

例如:@interface DemoView : UIView < UITableViewDelegate>

rather than delegate property you are creating.

而不是委托您正在创建的财产。

@property(retain)id < UITableViewDelegate> demoDelegate

@property(retain)id < UITableViewDelegate> demoDelegate