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.
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.
When you cast the self object then warning message disappears.
[speechSynth setDelegate:(id < NSSpeechSynthesizerDelegate >) self];
精彩评论