Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of Delegates in Swift [closed]

Tags:

I have been trying to learn how delegation with protocols work. I understood everything, but I can't think of when to use delegation other than when using table views and possibly scroll views.

In general, when is delegation used?

like image 205
Nikhil Sridhar Avatar asked Nov 09 '16 07:11

Nikhil Sridhar


People also ask

What is delegate in Swift with example?

The Delegate Pattern 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. One example is the UIApplicaitonDelegate in an iOS app.

What is delegate in closure?

If you look at some delegate methods (and nearly all dataSource methods), there is an expected return value. That means the delegating object is asking for the state of something. While a closure could reasonably maintain state or at least deduce state, this is really an object's role. Think about it.

What is the difference delegates and callbacks?

Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.

Why delegates are weak in Swift?

But if the delegate object is a class object, then you might want to make the protocol a class protocol and make it weak. In my opinion, making class delegates weak is only partially to alleviate the risk of a strong reference cycle. It also is a question of ownership.


1 Answers

What is Delegation?

First of all, you should know that Delegation Pattern is not exclusive for iOS world:

In software engineering, the delegation pattern is a design pattern in object-oriented programming that allows object composition to achieve the same code reuse as inheritance.

But working with delegation in the iOS world is so common, I assume that you can see many of classes that provide a delegation/datasource for giving the ability to provide properties or behaviors for the used instance. It is one of main mechanisms of how objects talk to each other in CocoaTouch.


Alternatives:

However, delegation is not the only way to let objects talk to each other in iOS, you might want to know that there are:

  • NotificationCenter.
  • KVO (Key-Value Observing).
  • Completion handlers/Callbacks (using closures).
  • Target-Action.

Remark: in case if you are interested in comparing between them, you might want to check the following articles:

  • Communication Patterns.
  • When to Use Delegation, Notification, or Observation in iOS.
  • Delegates vs Observers.

When to use Delegation?

So, the question is: "So why should I use delegation instead of those options?"

I will try to make it simple; I would suggest the use of delegation when you have one to one relationship between two objects. Just to make it clearer, the goal of talking a little bit about the NotificationCenter is to try to make sense when delegations are used:

NotificationCenter represents one to many relationship; Simply, it works as: posting (notifying) a notification on a specific event and observing (get notified about) this notification -- it could be observed anywhere else; Logically, that's what one to many relationship means. It is a representation of the Observer Pattern.


How to Apply Delegation?

For the purpose of simplifying, I would mention it as steps:

  1. Knowing the requirements: Each delegate has its own rules, listed in the delegate protocol which is a set of method signatures that you should implement for conforming this delegation.

  2. Conforming for the delegation: it is simply letting your class to be a delegate, by marking it. For instance: class ViewController: UIViewController, UITableViewDelegate {}.

  3. Connecting the delegate object: Marking your class to be a delegate is not enough, you need to make sure that the object you want to be confirmed by your class to give the required job to your class.

  4. Implementing the requirements: Finally, your class have to implement all required methods listed in the delegate protocol.


For Example

Does it sounds a little confusing? What about a real-world example?

Consider the following scenario:

Imagine that you are building an application related to playing audios. Some of the viewControllers should have a view of an audio player. In the simplest case, we assume that it should have a play/pause button and another button for, let's say, showing a playlist somehow, regardless of how it may look like.

So far so good, the audio player view has its separated UIView class and .xib file; it should be added as a subview in any desired viewController.

Now, how can you add functionality to both of the buttons for each viewController? You might think: "Simply, I will add an IBAction in the view class and that's it", at first look, it might sound ok, but after re-thinking a little bit, you will realize that it will not be applicable if you are trying to handle the event of tapping the button at the controller layer; To make it clear, what if each viewController implemented different functionality when tapping the buttons in the audio player view? For example: tapping the playlist in "A" viewController will display a tableView, but tapping it in the "B" viewController will display a picker.

Well, let's apply Delegation to this issue:

The "#" comments represents the steps of "How to Apply Delegation?" section.

Audio Player View:

// # 1: here is the protocol for creating the delegation protocol AudioPlayerDelegate: class {     func playPauseDidTap()     func playlistDidTap() }  class AudioPlayerView: UIView {     //MARK:- IBOutlets     @IBOutlet weak private var btnPlayPause: UIButton!     @IBOutlet weak private var btnPlaylist: UIButton!      // MARK:- Delegate     weak var delegate: AudioPlayerDelegate?      // IBActions     @IBAction private func playPauseTapped(_ sender: AnyObject) {         delegate?.playPauseDidTap()     }      @IBAction private func playlistTapped(_ sender: AnyObject) {         delegate?.playlistDidTap()     } } 

View Controller:

class ViewController: UIViewController {     var audioPlayer: AudioPlayerView?      // MARK:- Life Cycle     override func viewDidLoad() {         super.viewDidLoad()          audioPlayer = AudioPlayerView()         // # 3: the "AudioPlayerView" instance delegate will implemented by my class "ViewController"         audioPlayer?.delegate = self     } }  // # 2: "ViewController" will implement "AudioPlayerDelegate": extension ViewController: AudioPlayerDelegate {     // # 4: "ViewController" implements "AudioPlayerDelegate" requirments:     func playPauseDidTap() {         print("play/pause tapped!!")     }      func playlistDidTap() {         // note that is should do a different behavior in each viewController...         print("list tapped!!")     } } 


Quick Tip:

As one of the most popular examples of using delegation is Passing Data Back between View Controllers.

like image 151
Ahmad F Avatar answered Oct 22 '22 03:10

Ahmad F