Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of an iOS delegate?

Tags:

ios

I understand what a delegate does in iOS, and I've looked at sample code, but I'm just wondering about the advantages of this type of encapsulation (as opposed to including delegate methods in the primary object).

like image 657
Roy Avatar asked Aug 13 '11 20:08

Roy


People also ask

What is the purpose of delegate in Swift?

In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object.

What does the app delegate do?

Overview. Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.

What is delegate pattern in iOS?

The core purpose of the delegate pattern is to allow an object to communicate back to its owner in a decoupled way. By not requiring an object to know the concrete type of its owner, we can write code that is much easier to reuse and maintain.

What is delegate and protocol in iOS Swift?

In Swift, declaring a delegate property is just like declaring any other property and you specify the protocol name as the type of the property. You may notice the question mark syntax which indicates that it's a property with an optional value (there may or may not be an object assigned to it).


2 Answers

The advantage of the delegate design pattern is loose coupling. It enables class A (the delegate) to depend on class B (the delegating class) without class B having to have any knowledge of class A. This ensures that the dependency relationship is one-way only, rather than being circular.

It also forms the foundation (lower case "f") of Apple's frameworks because it allows them to invoke your code as appropriate when functionality specific to your application is required. For example, responding to a button tap or telling a table view how many sections there should be.

like image 144
John Topley Avatar answered Sep 20 '22 10:09

John Topley


Delegation is a design pattern not only used in iOS but many other languages. It enables you to hand values and messages over in your class hierarchy.

like image 25
Patrick Avatar answered Sep 23 '22 10:09

Patrick