I don't understand how to use delegates in Cocoa, but I know they are

Like many people, I am interested in Objective-C and Cocoa programming. I know conceptually what these delegates are, but I don't understand how to use them or when to use them. Here's some sample code:

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

      

I am setting the AppControler as a delegate to the SpeechSynthasizer. This means that the SpeSynthasizer tells hte AppControler what to do. But I don't understand this line:  [speechSynth setDelegate:self];

+2


source to share


4 answers


self

- the current object, therefore [speechSynth setDelegate:self]

sets the object delegate to the speechSynth

current object, i.e. your copy AppControler

(sic).



Edit: In addition to the code shown, yours AppControler

must implement a NSSpeechSynthesizerDelegate

protocol
for the messages you want to delegate to it.

+5


source


This means speechSynth will send all this message delegation to the AppController instance

NSSpeechSynthesizer instances will send speechSynthesizer: didFinishSpeaking to their delegates (make sure AppController implements this if you need to know when speech ends)



For more details on delegates check Delegation

+2


source


"This means that speSynthasizer tells hte AppControler what to do.

Indeed, the delegate usually makes the message.

An object with a delegate will ask the delegate how he should do something, or tell the delegate what he is about to do or has done. The delegate knows what to do, and the other object knows how to do them.

The delegate must implement methods that other objects expect. For example, the UIApplication delegate usually implements applicationDidFinishLaunching: because UIApplication knows how to launch, not what to do next. Any class that accepts a delegate will have a corresponding protocol or category that defines the methods that the delegate can implement.

+1


source


This makes speech that this particular "AppControler" instance (which is misspelled) should be its delegate.

0


source







All Articles