Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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):

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'

like image 632
Jeremy L Avatar asked Apr 30 '12 01:04

Jeremy L


2 Answers

Conform to the AVAudioPlayerDelegate protocol 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.

@interface ViewController : UIViewController <AVAudioPlayerDelegate> @end 
like image 73
CodaFi Avatar answered Sep 21 '22 17:09

CodaFi


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

[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

like image 39
Rajive Jain Avatar answered Sep 20 '22 17:09

Rajive Jain