About Cocoa delegate

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

About Cocoa delegate

stepharo
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)


Screen Shot 2014-07-16 at 10.47.04.pdf (271K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

EstebanLM
(if I understood fine your question):

because ObjC uses informal protocols to define delegates, and an informal protocol might or might not be complete (it just define a collection of methods that you might have). 
is informal because user could want to define just a part of the protocol (in the example, just windowShouldClose:) and not the many objects the delegate protocol defines. 
and it is not an abstract class with empty methods because then you force user to extend a particular class (and you cannot not use any arbitrary object as a delegate)

Esteban

ps: is possible that Cocoa uses delegates and we can use just blocks… I don’t know… but the delegating idea is to not pollute apps with thousands of subclasses of widgets with minimal variations (like something we know) :)

On 16 Jul 2014, at 10:48, stepharo <[hidden email]> wrote:

Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)

<Screen Shot 2014-07-16 at 10.47.04.pdf>

Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

stepharo
In reply to this post by stepharo
I found strange to force user to write

Before invoking a delegation method, make sure the delegate implements it by sending it a

respondsToSelector: message.

Cocoa Fundamentals Guide (TP40002974 6.0.0)
- (void)someMethod {

if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){

        if ( [delegate operationShouldProceed] ) {
            // do something appropriate

} }

}

I'm reading also on bindings (could be like valueHolder) and targetAction.
I think that I will have to read that several times :)

https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf

Stef

Cocoa Fundamentals Guide (TP40002974 6.0.0)
On 16/7/14 10:48, stepharo wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)


Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

EstebanLM
btw… while I think cocoa fundamentals is good reading, it is marked as a retired document. 
I think the replace doc is this one: 

https://developer.apple.com/library/mac/documentation/general/conceptual/CocoaEncyclopedia/CocoaEncyclopedia.pdf

(but since I still do not read it, I don’t know if is a good replacement)

Esteban

On 16 Jul 2014, at 10:58, stepharo <[hidden email]> wrote:

I found strange to force user to write

Before invoking a delegation method, make sure the delegate implements it by sending it a

respondsToSelector: message.

Cocoa Fundamentals Guide (TP40002974 6.0.0)
- (void)someMethod {

if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){

        if ( [delegate operationShouldProceed] ) {
            // do something appropriate

} }

}

I'm reading also on bindings (could be like valueHolder) and targetAction.
I think that I will have to read that several times :)

https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf

Stef

Cocoa Fundamentals Guide (TP40002974 6.0.0)
On 16/7/14 10:48, stepharo wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)



Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

bestlem
In reply to this post by stepharo

You don't have to write the check it is just  defensive programming you
can just do

  -(void)someMethod {
       if ( [delegate operationShouldProceed] ) {
              // do something appropriate

  } }

then if the delegate does not have operationShouldProceed you get a
selector not found error - which will crash the program. So similar to
smalltalk except that would get a debugger here.

So depends how certain you have a good delegate or if there is something
you can do if you don't.

Mark

On 16/07/2014 09:58, stepharo wrote:

> I found strange to force user to write
>
> Before invoking a delegation method, make sure the delegate implements
> it by sending it a
>
> respondsToSelector: message.
>
> Cocoa Fundamentals Guide (TP40002974 6.0.0)
>
> - (void)someMethod {
>
> if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){
>
>          if ( [delegate operationShouldProceed] ) {
>              // do something appropriate
>
> } }
>
> }
>
> I'm reading also on bindings (could be like valueHolder) and targetAction.
> I think that I will have to read that several times :)
>
> https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf
>
> Stef
>
> Cocoa Fundamentals Guide (TP40002974 6.0.0)
> On 16/7/14 10:48, stepharo wrote:
>> Hi
>>
>> In the quest for a better UI framework :), I was re rereading about
>> delegate in Cocoa.
>> A delegating object will delegate certain operations to a delegate
>> object (check the attachment).
>> Now I was wondering why this is not a simple delegation they wrote:
>>
>> The delegating object sends a message only if the delegate implements
>> the method. It makes this discovery by invoking the NSObject method
>> respondsToSelector: in the delegate first.
>>
>> Cocoa Fundamentals Guide (TP40002974 6.0.0)
>>
>


--
Mark


Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

stepharo
In reply to this post by EstebanLM
Thanks I will have a look.
Esteban my point is that it looks like delegates are like strategy but I wonder why all the strategies for a given facet
would not have to implement the protocol. To me having isKindOf: respondTo: sucks so when a framework forces it
this is even worse.

Stef

On 16/7/14 11:04, Esteban Lorenzano wrote:
btw… while I think cocoa fundamentals is good reading, it is marked as a retired document. 
I think the replace doc is this one: 

https://developer.apple.com/library/mac/documentation/general/conceptual/CocoaEncyclopedia/CocoaEncyclopedia.pdf

(but since I still do not read it, I don’t know if is a good replacement)

Esteban

On 16 Jul 2014, at 10:58, stepharo [hidden email] wrote:

I found strange to force user to write

Before invoking a delegation method, make sure the delegate implements it by sending it a

respondsToSelector: message.

Cocoa Fundamentals Guide (TP40002974 6.0.0)
- (void)someMethod {

if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){

        if ( [delegate operationShouldProceed] ) {
            // do something appropriate

} }

}

I'm reading also on bindings (could be like valueHolder) and targetAction.
I think that I will have to read that several times :)

https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf

Stef

Cocoa Fundamentals Guide (TP40002974 6.0.0)
On 16/7/14 10:48, stepharo wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)




Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

Eliot Miranda-2
In reply to this post by stepharo



On Wed, Jul 16, 2014 at 1:48 AM, stepharo <[hidden email]> wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Sad that this framework confuses delegation and forwarding.  What it describes as delegation is merely forwarding.  Delegation is quite different; self is bound to the delegator when running delegated methods.  When (as in Self) one object delegates to another, sends to self in the second object bind to methods in the first object, the delegator.  In forwarding, self is bound to the object the message is forwarded to (i.e. normal Smalltalk & ObjectiveC semantics).

--
best,
Eliot
Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

stepharo
In reply to this post by EstebanLM
I checked and the new doc does not refer to delegate.
I will read the target action and others

Stef

On 16/7/14 11:04, Esteban Lorenzano wrote:
btw… while I think cocoa fundamentals is good reading, it is marked as a retired document. 
I think the replace doc is this one: 

https://developer.apple.com/library/mac/documentation/general/conceptual/CocoaEncyclopedia/CocoaEncyclopedia.pdf

(but since I still do not read it, I don’t know if is a good replacement)

Esteban

On 16 Jul 2014, at 10:58, stepharo [hidden email] wrote:

I found strange to force user to write

Before invoking a delegation method, make sure the delegate implements it by sending it a

respondsToSelector: message.

Cocoa Fundamentals Guide (TP40002974 6.0.0)
- (void)someMethod {

if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){

        if ( [delegate operationShouldProceed] ) {
            // do something appropriate

} }

}

I'm reading also on bindings (could be like valueHolder) and targetAction.
I think that I will have to read that several times :)

https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf

Stef

Cocoa Fundamentals Guide (TP40002974 6.0.0)
On 16/7/14 10:48, stepharo wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Cocoa Fundamentals Guide (TP40002974 6.0.0)




Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

stepharo
In reply to this post by Eliot Miranda-2
:)
I was not even at this level. Many people confuses delegation and forwarding. I remember that I helped removing some errors in books I reviewed.

Stef

On 16/7/14 20:07, Eliot Miranda wrote:



On Wed, Jul 16, 2014 at 1:48 AM, stepharo <[hidden email]> wrote:
Hi

In the quest for a better UI framework :), I was re rereading about delegate in Cocoa.
A delegating object will delegate certain operations to a delegate object (check the attachment).
Now I was wondering why this is not a simple delegation they wrote:

The delegating object sends a message only if the delegate implements the method. It makes this discovery by invoking the NSObject method respondsToSelector: in the delegate first.

Sad that this framework confuses delegation and forwarding.  What it describes as delegation is merely forwarding.  Delegation is quite different; self is bound to the delegator when running delegated methods.  When (as in Self) one object delegates to another, sends to self in the second object bind to methods in the first object, the delegator.  In forwarding, self is bound to the object the message is forwarded to (i.e. normal Smalltalk & ObjectiveC semantics).

--
best,
Eliot

Reply | Threaded
Open this post in threaded view
|

Re: About Cocoa delegate

stepharo
In reply to this post by bestlem
tx mark.
I know now I found strange that they push that. Now apparently this is
not in the new doc.

On 16/7/14 16:56, Mark Bestley wrote:

>
> You don't have to write the check it is just  defensive programming
> you can just do
>
>  -(void)someMethod {
>       if ( [delegate operationShouldProceed] ) {
>              // do something appropriate
>
>  } }
>
> then if the delegate does not have operationShouldProceed you get a
> selector not found error - which will crash the program. So similar to
> smalltalk except that would get a debugger here.
>
> So depends how certain you have a good delegate or if there is
> something you can do if you don't.
>
> Mark
>
> On 16/07/2014 09:58, stepharo wrote:
>> I found strange to force user to write
>>
>> Before invoking a delegation method, make sure the delegate implements
>> it by sending it a
>>
>> respondsToSelector: message.
>>
>> Cocoa Fundamentals Guide (TP40002974 6.0.0)
>>
>> - (void)someMethod {
>>
>> if ( [delegate respondsToSelector:@selector(operationShouldProceed)] ){
>>
>>          if ( [delegate operationShouldProceed] ) {
>>              // do something appropriate
>>
>> } }
>>
>> }
>>
>> I'm reading also on bindings (could be like valueHolder) and
>> targetAction.
>> I think that I will have to read that several times :)
>>
>> https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf 
>>
>>
>> Stef
>>
>> Cocoa Fundamentals Guide (TP40002974 6.0.0)
>> On 16/7/14 10:48, stepharo wrote:
>>> Hi
>>>
>>> In the quest for a better UI framework :), I was re rereading about
>>> delegate in Cocoa.
>>> A delegating object will delegate certain operations to a delegate
>>> object (check the attachment).
>>> Now I was wondering why this is not a simple delegation they wrote:
>>>
>>> The delegating object sends a message only if the delegate implements
>>> the method. It makes this discovery by invoking the NSObject method
>>> respondsToSelector: in the delegate first.
>>>
>>> Cocoa Fundamentals Guide (TP40002974 6.0.0)
>>>
>>
>
>