Summary of Protocol & Closure in iOS

Tram Ho

1. Introduction

1.1. Protocol

A brief outline of the protocol definition is as follows:

  • is an interface type
  • abstract nature
  • declare properties vs methods
  • do not define them
  • implementable into class/struct/enum
  • Implement to many, not one
  • can be viewed as a data type

And you will learn more in this article: Protocol in 10 minutes

1.2. Closure

Closure is a block of code, which can be disassembled for reuse. In simple terms, Closure is a function, but anonymous. We can assign a Closure to a variable and reuse it like any other value type

  • Points to pay attention to:
    • block code
    • function
    • Noname
    • variable

and you will learn more fully at this article: Closure in 10 minutes

2. Apply

Here, I will list the cases in which you can thoroughly apply Protocol & Closure. And those are very basic problems in iOS programming.

2.1 Custom View

  • Custom View is to create new UI Controls, so that the application interface is in accordance with the interface design of the IOS application.
  • Interface designs are often complex and do not use basic UI Controls.

We will apply Protocol to the data transmission (Passing Data) from Custom View to ViewController. For detailed instructions, see the link below:

2.2. TableView

You only need to be proficient in TableView to be proficient in iOS programming.

This is one of the legendary UI Controls of iOS. And accompanying TableView are its Delegate & Datasource Protocol, which you must be able to use. This Protocol design pattern is used for most of the remaining UI Controls, such as CollectionView, PickerView, MapView…

Details of this article can be found at the link below:

2.3. Delegate Pattern

The article in the link below will specifically explain the application of Protocol in the problem of passing Data. With the name Delegate & Datasource you will be more familiar. Also one of the skills you need to acquire

2.4. MVVM

This is the most used model for the current IOS Project. We do not go into the structural analysis of MVVM, but focus on the application of Closure for this model. Closure is very effective in making CallBack from the ViewModel to the View/ViewController. This is also one of the basic techniques that an IOS Dev is required to do. You can read about it at the link below:

2.5. Connect Networking & Core API

This is one of the classic applications of Closure. Closure’s strength is its asynchronous handling. And will be applied to interacting with the API from app. You can follow up through a few basic articles below:

3. Discrimination

In this third part, we will learn about the analysis of the similarities and differences of these two forces Protocol & Closure.

3.1. Passing Data

Passing Data is not always simple. You need to understand the usages of Protocol & Closure in dealing with similarity problems in data transmission. From there you can see the similarities of these two methods.

Details of this article in the link below:

3.2 Asynchronous

From the asynchronous problem, we can see the difference between Protocol & Closure. With each solution we need to note that each problem belongs to the nature of each type. Then you will easily learn the code of other people, of libraries … that apply Protocol & Closure to asynchronous processing.

To get rid of the mechanical way of coding, you can refer to the article link below:

3.3. Delegate vs Closure callback

Finally we will know when to choose Delegate or Closure. Pros & cons. In the article below, I have also summarized the common cases encountered in the process of iOS programming

Ending

Through Above, you will get an overview of Protocol & Closure in IOS programming. This is also a common problem for a newbie when new to the project. Hope this article will be of help to you.

Share the news now