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 :)

Arseny
  • 7,251
  • 4
  • 37
  • 52
Artemix
  • 8,497
  • 14
  • 48
  • 75

2 Answers2

3

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
  • 6,978
  • 2
  • 17
  • 18
  • And there is no reason you can't have delegates in C++ or Java. – James Jul 15 '10 at 14:34
  • So, when I do something like myObject.delegate = myDelegateObject, exactly what am'I doing? – Artemix Jul 15 '10 at 19:42
  • 1
    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 Jul 15 '10 at 21:16
  • So.. is a way to save code or something like that, ok I think I get it. – Artemix Jul 16 '10 at 22:02
2

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