What are delegates?

3

In iOS programming, it is common to use structures that act as delegates .

One of them, for example, is UITextFieldDelegate . This class, according to the documentation , informs the implementer of important events of UITextField , as if user started typing or completed it.

I understand the use of UITextFieldDelegate , but not what those delegates are, and why they are needed.

  • Is there a more concrete definition?
  • Is it a design pattern? An architecture?
  • Is it unique to iOS programming or is it implemented in other technologies?
  • Is it an implementation of a kind of event-oriented programming?
asked by anonymous 25.02.2018 / 15:19

1 answer

2

Much of this is more or less answered in What's the difference between a lambda expression, a closure, and a delegate? .

Roughly is a pointer to a function, it's an indirection , so you can place the function in a variable and make the code flexible to what it will execute, you can parameterize what will be done by doing callback . But it may have something else, you may have the mechanism required to variables, or serve multicast . You have several ways to implement it.

Understand What is Design Pattern? . Everything you use is a design pattern. Some are recipes for other cake are ready mechanisms, until variable is design standard. It is a design pattern .

It's used everywhere , although not all languages have a ready engine, and some use a different name. In C # . And in JavaScript . Now has Java , but when it did not it was possible to do too, except that you had to write a huge code.

This is a basic technique for creating events and where events are used as a basis for programming can be event-oriented , but a delegate is not an event . Just note that this paradigm is secondary and alone it does not serve much, nothing real.

    
25.02.2018 / 17:17