gpt4 book ai didi

objective-c - 我不明白如何在 Cocoa 中使用委托(delegate),但我知道它们是什么

转载 作者:行者123 更新时间:2023-12-03 16:24:55 27 4
gpt4 key购买 nike

像许多人一样,我对 Objective - C 和 Cocoa 编程感兴趣。我从概念上知道它是什么委托(delegate),但我不明白如何使用它们或何时使用它们。这是一些示例代码:

#import "AppControler.h"


@implementation AppControler

-(id)init
{
[super init];
NSLog(@"init");

speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
//
[speechSynth setDelegate:self];
voiceList = [[speechSynth availableVoices] retain];

return self;
}

我将AppControler 设置为speechSynthasizer 的委托(delegate)。这意味着 SpeechSynthasizer 正在告诉 AppControler 做什么。但我不明白这一行: [speechSynth setDelegate:self];

最佳答案

self 是当前对象,因此 [speechSynth setDelegate:self]speechSynth 对象的委托(delegate)设置为当前对象,即您的 AppControler (原文如此)实例。

编辑:除了显示的代码之外,您的 AppControler 还应该实现 NSSpeechSynthesizerDelegate protocol用于您希望委托(delegate)给它的消息。

关于objective-c - 我不明白如何在 Cocoa 中使用委托(delegate),但我知道它们是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2611941/

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