You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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.
5
7
6
8
## Problem
7
-
Pass data between classes
9
+
How does delegate even work?
10
+
11
+
> **Purpose of Delegate:** Communicate/Pass Data between objects
8
12
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.
10
14
11
15
### 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`.
firstVC.delegate?.passData(data: "A bunch of contracts")
68
+
// "The CEO gave me a bunch of contracts"
66
69
```
67
70
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.
69
72
73
+
### UITableView in ViewController
74
+
```swift
75
+
importUIKit
70
76
71
-
> **Analogy:** Imagine `firstVC` is an operating system (delegator) while `secondVC` is the class (delegate) that receives information from the OS.
// 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.
[The Complete Understanding of Delegate and DataSource](https://blog.bobthedeveloper.io/the-complete-understanding-of-swift-delegate-and-data-source-9c91ecd7f1)
79
101
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)
81
103
82
104
83
105
## 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.
87
107
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.
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)
Copy file name to clipboardExpand all lines: source-code/4000_protocol_oriented_swift/4000_delegate_data_source.playground/playground.xcworkspace/contents.xcworkspacedata
0 commit comments