The basic basis of Swift's Delegate

The basic basis of Swift's Delegate

I will summarize the basics of Delegate, assuming that a friend who is a non-engineer and is studying Swift recently asks "What is Delegate?".

Delegate

The Japanese translation of the delegate is "delegation". The meaning of delegation is to hand over or leave it to us.

Delegate in swift will also be a delegation feature!

In a nutshell, there are many explanations that say, "One class can let an instance of another class do the work."

1. Who will be the delegate

Who = "Class A" Will be.

In other words, a class takes care of itself.

In the case of tableview, the "UITableView class" leaves its own processing to others.

2. What do you leave the delegate to?

What = "In class B" Will be.

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
    }

When creating a tableView with swift, I think that it is common to use this notation.

In this example, "delegate" is left to "self"! And here, self means "view controller" that describes this code. In other words, it's up to the "view controller" that has this code!

3. What does the delegate entrust?

What = "Processing yourself (class A)" Will be.

And this process is specifically called "delegate method". Therefore, officially, "processing in its own delegate method" is delegated (delegated) to another class instance.

If it is a table view

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}

Etc. correspond to the delegate method.

Summary

-Delegate has a function that "a certain class A can entrust processing (a delegate method of class A) to an instance of class B."

Recommended Posts

The basic basis of Swift's Delegate
The basic basis of Swift dialogs
The basic basis of Swift custom cells
The basics of Swift's TableView
Understand the basic mechanism of log4j2.xml
Basic basis of Android asynchronous processing "AsyncTask"
[Swift] Vaguely grasp the flow of Delegate
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
Image processing: The basic structure of the image read by the program
The world of clara-rules (1)
The world of clara-rules (3)
Basic format of Dockefile
[Swift] This is the solution! Illustration of Delegate implementation
The world of clara-rules (5)
The idea of quicksort
The idea of jQuery
Review the basic knowledge of ruby that is often forgotten
I tried to summarize the basic grammar of Ruby briefly
Aiming for a basic understanding of the flow of recursive processing
Basic methods of Ruby hashes
About the handling of Null
Docker monitoring-explaining the basics of basics-
Basic knowledge of SQL statements
Basic methods of Ruby arrays
[Docker] Introduction of basic Docker Instruction
About the description of Docker-compose.yml
Understand the basics of docker
The play of instantiating java.lang.Void
Explanation of the FizzBuzz problem
Super basic usage of Eclipse
Median of the three values
[Ruby] List of basic commands
The illusion of object orientation
Summary of basic functions of ImageJ
Switch the version of bundler
Review of Ruby basic grammar
The point of addiction when performing basic authentication with Java URLConnection