views:

99

answers:

3

I was coding some stuff on objetive c.. but I still dont get it, I dont know/understand what a delegate is at all.

Maybe cuz my main programming language is C++ and Java... dont know.

I searched the web looking for an ENGLISH explanation, but, seems like I dont speak english :)

A: 

Function pointer in C++ is quite similar to a delegate.

bjornhol
In languages like C#, yes. In Objective-C, not really.
robinjam
I agree that a function pointer could be the simplest form of a non-object-oriented delegate.
James Roth
+3  A: 

A delegate is an instance of an object that implements a bunch of what C programmers call callbacks, but in an object-oriented way. Like most new concepts, it is really just an old convention renamed and obfuscated.

There are some nice examples here on wikipedia.

James Roth
And there is no reason you can't have delegates in C++ or Java.
James Roth
So, when I do something like myObject.delegate = myDelegateObject, exactly what am'I doing?
Artemix
You are telling myObject to call the methods implemented by myDelegateObject whenever it needs to be fed information or when certain events happen. Those events should be documented in the docs for myObject's class. myDelegateObject can also have other methods and be designed to do other things beyond what myObject needs as a delegate.
James Roth
So.. is a way to save code or something like that, ok I think I get it.
Artemix
+2  A: 

In Objective-C, a delegate is an object that conforms to a specific protocol, that another object can rely on for specific functionality. It's a different concept to delegation in lots of other languages, and it can get confusing because the terminology is the same.

Here's an article I found rather useful when I started programming in Objective-C:

http://developer.apple.com/mac/library/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

robinjam