Skip to content

Commit 47734a2

Browse files
author
Bob Lee
committed
Prepped the delegate pattern
1 parent d805852 commit 47734a2

File tree

13 files changed

+64
-133
lines changed

13 files changed

+64
-133
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Conclusion
2+
So far, we've looked into 4 major popular functions that are commonly used in iOS development. However, remember there are a lot more functions available. I'm not asking you to memorize all other functions. In fact, there is an open source library that has a collection of functions. The purpose is to understand what's going to underneath the hood instead of being a code monkey. As you long as you have the ability to read and understand someone's source code, you are good to go. In fact, depending on your circumstances, you have the ability to create a function for your own need. That's it. It is curiosity that distinguishes a great developer from a good developer.
3+
4+
Recently, I've been working with RxSwift with my personal projects. RxSwift has hundreds of functions which are known as operators. Hopefully, I can make a course out of it in the future because it certainly disrupts the way code has been written in iOS development.
5+
6+
7+
## Resource
8+
[Swiftz - Functional programming in Swift](https://github.com/typelift/Swiftz)
Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Delegate
22

33
## Introduction
4-
Welcome to the last lesson of Protocol Oriented Swift. Passing data between classes using the delegate pattern is the fundamental way we, developers, receive information from the operating system. For instance, the user opens your app through a push notification, the iOS notifies and send information about the launch method to a class called, `AppDelegate` in which we have access to. This lesson is not about protocol oriented programming. Instead, you will utilize `protocols` to send data between classes or structs.
4+
Welcome to one of the most confusing topics of all time. You might have heard about the delegate pattern. Perhaps, you've seen, `tableView.delegate = self`. I've discovered a lot of developers have no idea how to explain what goes underneath. They just simply copy and paste as if it just works. In fact, I was one of them for solid 6 months when I first started learning iOS. Today, however, you are with me, Bob the Developer.
5+
6+
Do not worry about its vague terminology, called "delegate". This is a pattern that is used to communicate or send data between objects that are created with classes and structs. That's it. With that in mind, let me explain this magic for you.
57

68
## Problem
7-
Pass data between classes
9+
How does delegate even work?
10+
11+
> **Purpose of Delegate:** Communicate/Pass Data between objects
812
9-
> **Advice:** You just have to memorize it. It's a gift from Apple engineers. Take it. Do not question why it works. But, find out why it is useful. You might have to watch this video multiple times to get yourself comfortable.
13+
In this tutorial, you will learn how to send data from `FirstVC` to `SecondVC` using the delegate pattern.
1014

1115
### Design Protocol
12-
Create a protocol called, `PassDataDelegate`. It contains a method that takes `data` whose type is in `String` and returns `String`.
16+
Create a protocol called, `PassDataDelegate`. It contains a method that takes `data` whose type is in `String`.
1317

1418
```swift
1519
protocol PassDataDelegate {
16-
func passData(data: String) -> String
20+
func passData(data: String)
1721
}
1822
```
1923

@@ -33,13 +37,12 @@ FirstVC().delegate?.passData(data: "Bob") // nil
3337
```
3438

3539
### Design Delegate (Receiver)
36-
Create `SecondVC` that conforms to `PassDataDelegate`. `SecondVC` must contain `passData(data: String)`.
40+
Create `SecondVC` that conforms to `PassDataDelegate`. `SecondVC` must contain `passData(data: String)` due to the protocol.
3741

3842
```swift
3943
class SecondVC: PassDataDelegate {
40-
func passData(data: String) -> String {
41-
print("Something happened")
42-
return "You've entered \(data)"
44+
func passData(data: String) {
45+
print("The CEO gave me \(data)")
4346
}
4447
}
4548
```
@@ -50,39 +53,56 @@ let firstVC = FirstVC()
5053
let secondVC = SecondVC()
5154
```
5255

53-
Set `firstVC.delegate` to `secondVC`. It is possible since `secondVC` conforms to `PassDataDelegate`.
56+
Set `firstVC.delegate` to `secondVC`. It is possible since `secondVC` conforms to `PassDataDelegate` because Swift Protocol can be used as one's type.
5457

5558
```swift
56-
firstVC.delegate = secondVC // you are sending data to classTwo
59+
firstVC.delegate = secondVC
5760
```
5861

59-
When you set the relationship as above, you may call a method from `firstVC` along with its custom data. You may use the data in `secondVC`.
62+
When you set the relationship as above, you may call the `passData` method that resides in the `secondVC` object from the `firstVC`object.
6063

64+
### Assign Delegate
6165
```swift
62-
//: Assign Delegate
6366
firstVC.delegate = secondVC
64-
firstVC.delegate?.passData(data: "Hello, 1231231")
65-
// "You've entered Hello, 1231231"
67+
firstVC.delegate?.passData(data: "A bunch of contracts")
68+
// "The CEO gave me a bunch of contracts"
6669
```
6770

68-
When the the `passData` method is called from the `firstVC`, `secondVC` contains now receives the data and may execute any lines of code.
71+
In the example above, `firstVC` is calling the `passData` method and it passes `data`, called, "A bunch of contracts". When it occurs, the full implementation of the method that resides in `secondVC` get called. Thus, the `print` function is also get executed.
6972

73+
### UITableView in ViewController
74+
```swift
75+
import UIKit
7076

71-
> **Analogy:** Imagine `firstVC` is an operating system (delegator) while `secondVC` is the class (delegate) that receives information from the OS.
77+
class BobViewController: UIViewController, UITableViewDelegate {
78+
let tableView = UITableView()
7279

73-
### Source code
80+
override func viewDidLoad() {
81+
super.viewDidLoad()
82+
tableView.delegate = self
83+
}
7484

85+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
86+
print(tableView)
87+
// Hey BobViewController/Delegate, do something with the data I've given you
88+
// Download Image...
89+
// ...
90+
}
91+
}
92+
```
93+
94+
In the example above, `tableView` is the delegator/sender/CEO, while the object of `BobViewController` is the delegate/receiver/Secretary. The method, `func tableView(_talbeView: UITableView, didSelectRowAt indexPath: IndexPath)`, is called by the `tableView`. Yet, the full implementation is executed within the `BobViewController` object.
95+
96+
### Source code
7597
[4004_delegate.playground](https://www.dropbox.com/sh/s2ttgjx7yhucyyu/AAC9ROxz-_X8yKy2wxJK_QLfa?dl=0)
7698

7799
### Resources
78-
[Introduction to Delegate in Swift]
100+
[The Complete Understanding of Delegate and DataSource](https://blog.bobthedeveloper.io/the-complete-understanding-of-swift-delegate-and-data-source-9c91ecd7f1)
79101

80-
[Introduction to Delegate in Swift]: https://blog.bobthedeveloper.io/the-meaning-of-delegate-in-swift-347eaa9674d
102+
[Introduction to Delegate in Swift](https://blog.bobthedeveloper.io/the-meaning-of-delegate-in-swift-347eaa9674d)
81103

82104

83105
## Conclusion
84-
The delegate pattern is tough to describe how it exactly works because it's a tool provided by Apple engineers. As I said, it works because smart individuals have designed the feature for us so we just have to read manual and implement. If you feel uncomfortable with the setup, I recommend you to watch this video multiple times and practice on your own. You may use a real app to pass data between view controllers.
85-
86-
There is one more thing called, `data source`. If you wish to learn more how to create custom delegate and data source, you may join the next course, The UIKIt Fundamentals with Bob.
106+
You've learned the delegate pattern is used to pass data between objects. In fact, you've learned how to pass data from`FirstVC` to `SecondVC` in one direction. Simply put, you've learned passing data from the CEO to the secretary. In the following lesson, you will learn that you can also send data backward, the secretary to the CEO using `data source`. You will find out.
87107

88-
> **Note:** Learn Swift with Bob is available on [Udemy](https://udemy.com/learn-swift-with-bob/). If you wish to receive a discount link, you may sign up [here](https://goo.gl/RR4K27).
108+
If you have not grasped the concept of delegate, I recommend you to ask questions on Udemy, Slack, or any other platforms. I've also attached additional articles for you to take a look at. You've got to know the principle because in the next course, you will learn how this pattern is to communicate between iOS, the operating system, and us, developers.

course/swift4/conclusion.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
# Conclusion
2-
So far, we've looked into 4 major popular functions that are commonly used in iOS development. However, remember there are a lot more functions available. I'm not asking you to memorize all other functions. In fact, there is an open source library that has a collection of functions. The purpose is to understand what's going to underneath the hood instead of being a code monkey. As you long as you have the ability to read and understand someone's source code, you are good to go. In fact, depending on your circumstances, you have the ability to create a function for your own need. That's it. It is curiosity that distinguishes a great developer from a good developer.
3-
4-
Recently, I've been working with RxSwift with my personal projects. RxSwift has hundreds of functions which are known as operators. Hopefully, I can make a course out of it in the future because it certainly disrupts the way code has been written in iOS development.
5-
6-
7-
## Resource
8-
[Swiftz - Functional programming in Swift](https://github.com/typelift/Swiftz)

source-code/.DS_Store

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

source-code/4000_protocol_oriented_swift/4000_delegate_data_source.playground/Contents.swift

Lines changed: 0 additions & 64 deletions
This file was deleted.

source-code/4000_protocol_oriented_swift/4000_delegate_data_source.playground/contents.xcplayground

Lines changed: 0 additions & 4 deletions
This file was deleted.

source-code/4000_protocol_oriented_swift/4000_delegate_data_source.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 0 additions & 7 deletions
This file was deleted.
-12.1 KB
Binary file not shown.

source-code/4000_protocol_oriented_swift/4004_delegate.playground/Contents.swift

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,53 @@
88

99
---
1010
*/
11-
12-
//: > **Purpose of Delegate:** Communicate between objects
13-
11+
//: > **Purpose of Delegate:** Communicate/Pass Data between objects
1412

1513
//: **Goal:** Send data from `FirstVC` to `SecondVC`
1614

15+
//: ### CEO and Secretary Relationship
1716
//: Design Protocol
1817
protocol PassDataDelegate {
1918
func passData(data: String)
2019
}
21-
22-
2320
//: Design Delegator (Sender)
2421
class FirstVC {
2522
func drawCircle() {}
2623
var delegate: PassDataDelegate?
2724
}
2825

29-
FirstVC().delegate?.passData(data: "A bunch of contracts")
30-
31-
26+
FirstVC().delegate?.passData(data: "a bunch of contracts")
3227
//: Design Delegate (Receiver)
3328
class SecondVC: PassDataDelegate {
3429
func passData(data: String) {
3530
print("The CEO gave me \(data)")
31+
// Update the UI
3632
}
3733
}
38-
39-
4034
//: Create Objects
4135
let firstVC = FirstVC()
4236
let secondVC = SecondVC()
4337

4438
//: Assign Delegate
4539
firstVC.delegate = secondVC
46-
firstVC.delegate?.passData(data: "A bunch of contracts")
47-
48-
49-
50-
40+
firstVC.delegate?.passData(data: "a bunch of contracts")
5141
//: Practical Example in iOS
5242
import UIKit
5343

5444
class BobViewController: UIViewController, UITableViewDelegate {
5545
let tableView = UITableView()
46+
5647
override func viewDidLoad() {
5748
super.viewDidLoad()
5849
tableView.delegate = self
5950
}
6051

6152
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
62-
print(indexPath)
53+
print(tableView)
54+
// Hey BobViewController/Delegate, do something with the data I've given you
55+
// Download Image...
56+
// ...
6357
}
6458
}
65-
66-
67-
//: tableView.delegate.tableView(_ tableView: UITableView, didSelectRorwAt indexPath: IndexPath)
68-
69-
70-
// UITableView is the delegator/CEO/sender
71-
// BobViewController is the delegate/Secretary/receiver
72-
73-
74-
59+
//: - `UITableView` is the delegator/CEO/sender
60+
//: - `BobViewController` is the delegate/Secretary/receiver

0 commit comments

Comments
 (0)