What are the delegates?

Asked

Viewed 304 times

6

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

One of them, for example, is the UITextFieldDelegate. This class, according to documentation, informs the implementor of important events of the UITextField, how the user started typing or finished it.

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

  • There is a more concrete definition?
  • Is it a Pattern design? An architecture?
  • Exclusive to iOS programming or implemented in other technologies?
  • It is an implementation of a kind of event-oriented programming?
  • You can think of a delegate as a pointer * for a function fun(), he keeps a reference so that it can be invoked in the future.

  • This comes from a doubt of Swift, Objective-C?

  • Comes from Swift.......

  • Can put the tag?

1 answer

5


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

Roughly speaking it’s a pointer to a function, it’s a indirect, so you can put the function in a variable and make the code flexible to what it will execute, can parameterize what will be executed by making a callback. But he may have something else, he may have the mechanism necessary to enclosing variables, or serve as multicast. There are several ways to implement.

Understand What is Design Pattern?. All it uses is a design pattern. Some are cake recipes others are ready mechanisms, until variable is design standard. It is a Pattern design.

It’s used where it’s needed, although not all languages have a ready mechanism, and some use another name. In C#. And javascript. Now has in Java, but when it was not possible to do it too, only that it had to write a huge code.

It’s a basic technique to create events and where events are used as the basis of programming can be event-oriented, but a delegate is not an event. Just note that this paradigm is secondary and alone he is not good for much, nothing real.

  • great references. thank you very much ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.