setDelegate:self生成警告标志

时间:2011-08-10 22:21:45

标签: objective-c delegates

我正在尝试学习Objective C并且在我的一个版本的代码中出错,我不知道如何解决它。 代码:

 //  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;
    }

来自[speechSynth setDelegate:self];我收到错误:将'AppController *'发送到不兼容类型'id<的参数NSSpeechSynthesizerDelagate>”。 该程序编译一个警告标志,似乎运行正常。我已经将我的代码与作者的代码进行了比较,但没有发现任何差异,我的搜索都没有表明我应该在这一行上出错。这本书是为Xcode 3编写的,我使用的是Xcode 4.0.2。

任何建议或指出我正确的方向将不胜感激。感谢。

2 个答案:

答案 0 :(得分:14)

Xcode警告您setDelegate方法需要实现NSSpeechSynthesizerDelagate协议的类的实例。现在,你有,但你可能只是忘记宣布你有。在您的班级声明中,更改

@class AppController : NSObject

@class AppController : NSObject<NSSpeechSynthesizerDelegate>

告诉世界“我服从NSSpeechSynthesizerDelegate!”,并使警告保持沉默。你永远不会知道 - 你可能会收到警告,你已经忘记实现一些非可选的委托方法,并在某个地方保存一个烦人的bug。

答案 1 :(得分:2)

当您投射自我对象时,警告消息将消失。

[speechSynth setDelegate:(id < NSSpeechSynthesizerDelegate >) self];
相关问题