Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setDelegate:self generates a caution flag

I am trying to learn Objective C and have an error in the code for one of my lessions and I do not know how to solve it. Code:

 //  AppController.m

 #import "AppController.h"

 @implementation AppController

 - (id)init
    {
         [super init];
         speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
         [speechSynth setDelegate:self];
         voiceList = [[NSSpeechSynthesizer availableVoices] retain];
         Return self;
    }

From [speechSynth setDelegate:self]; I get the error: Sending 'AppController *' to parameter of incompatible type 'id< NSSpeechSynthesizerDelagate>'. The program compiles with a caution flag and seems to run correctly. I have compared my code with the author's code and can find no differences and none of my searches have indicated I should get an error on this line. The book was written for Xcode 3 and I am using Xcode 4.0.2.

Any suggestions or pointing me in the right direction would be greatly appreciated. Thanks.

like image 606
Aubrey Todd Avatar asked Aug 10 '11 22:08

Aubrey Todd


2 Answers

Xcode is warning you that the setDelegate method expects an instance of a class that has implemented the NSSpeechSynthesizerDelagate protocol. Now, you have, but you've probably just forgotten to declare that you have. In your class declaration, change

@class AppController : NSObject

to

@class AppController : NSObject<NSSpeechSynthesizerDelegate>

to tell the world "I obey NSSpeechSynthesizerDelegate!", and silence the warning. You never know - you might get warned that you've forgotten to implement some non-optional delegate methods, and save yourself an annoying bug somewhere down the line. 

like image 173
Adam Wright Avatar answered Nov 21 '22 11:11

Adam Wright


When you cast the self object then warning message disappears.

[speechSynth setDelegate:(id < NSSpeechSynthesizerDelegate >) self];
like image 36
aytek Avatar answered Nov 21 '22 13:11

aytek