gpt4 book ai didi

iphone - 如何解决警告: Sending 'ViewController *const __strong' to parameter of incompatible type 'id?

转载 作者:IT王子 更新时间:2023-10-29 07:43:00 26 4
gpt4 key购买 nike

以下代码给出了 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];

如何解决?我在 ViewController 中有这段代码viewDidLoad 的实例方法.还有另一个类似的问题,但那是一个类方法:Incompatible pointer types assigning to 'id<AVAudioPlayerDelegate>' from 'Class'

最佳答案

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

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@end

关于iphone - 如何解决警告: Sending 'ViewController *const __strong' to parameter of incompatible type 'id<AVAudioPlayerDelegate>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com