Tutorial: delegates and delegation in Swift 4

The original article – Understanding Delegates and Delegation in Swift 4 was published on appcoda.com.

[Download Xcode 9 project with full Swift 4 source from GitHub.]

Introduction

I’m going to talk about “delegates” and “delegation.” I’ll lead you through a simple example of implementing the delegation design pattern in Swift 4, with full source code. My intent here is to show you how delegation works without getting bogged down in some crazy complex example. To help you become the best of the best, I’m going to introduce you to one of the greatest design tools to aid in object-oriented software development, UML. I’ll show you a UML diagram that I drew up to design and document the implementation of the delegation design pattern used in the sample app we’ll build together. Download the Xcode 9 project with full Swift 4 source from GitHub to follow along.

(Note: compare this post’s approach of using delegation with my next post’s approach of using NSNotificationCenterto accomplish the same goal.)

I’ll show you how to build a user interface (UI) helper, a class that downloads a file at a specified URL. Most importantly, I’ll show you how, through delegation, a UIViewController subclass can be notified by the helper that an image file has finished downloading, and then the view controller can display the image on screen. For the sake of simplicity and clarity, we’ll pretend that Swift has minimal support for downloading a file from a URL. We’ll manually wire up the notification that the file has finished downloading using the delegation design pattern. Here’s the app we’ll build:

Continue reading “Tutorial: delegates and delegation in Swift 4”

Tutorial: delegates and delegation in Objective-C

[Download Xcode 9 project with full Objective-C source from GitHub.]

Introduction

I’m going to talk about “delegates” and “delegation.” I’ll lead you through a simple example of implementing the delegation design pattern in Objective-C, with full source code, and then show you a more sophisticated scenario. My intent here is to show you how delegation works without getting bogged down in some crazy complex example. Download the Xcode 9 project with full Objective-C source from GitHub to follow along.

I’ll show you how to build a user interface (UI) component, a status/progress indicator, which you can display on screen for processing-intensive tasks… AND I’ll show you how you can customize the behavior of the indicator by using delegation. For example, when the indicator starts, you could disable your UI; when the indicator stops, you could re-enable your UI; and, when the user taps the indicator, you could cancel processing-intensive tasks. Here’s the app we’ll build:

Continue reading “Tutorial: delegates and delegation in Objective-C”

Understanding Swift 4 protocols and using them in your apps

NOTE: Learn all about protocol-oriented programming in Swift here, here, and here.

[Download two Xcode 9 playgrounds with full Swift 4 source from GitHub.]

We’re going to talk about “protocols” in the Swift 4 language today. I’ll explain them conceptually, and then we’ll start coding protocols with a simple example. We’ll then create our own versions of the Apple built-in Equatable and Comparable protocols, and apply them to two real-world classes, one for tracking financial securities and one for representing geometric lines/vectors. Finally, we’ll test our geometric “Line” class in a type of Swift playground that supports rendering user interface components (like UIView) live in the simulator. But first, please ponder the layman’s definition of the word “protocol” before moving on:

… The official procedure or system of rules governing affairs of state or diplomatic occasions. …

The accepted or established code of procedure or behaviour in any group, organization, or situation. …

A procedure for carrying out a scientific experiment…

Swift Protocols

Apple’s “The Swift Programming Language (Swift 4.0.3)” documentation states:

Continue reading “Understanding Swift 4 protocols and using them in your apps”

Using Swift extensions to manage complexity, improve readability, and increase extensibility (UICollectionView, protocols, and delegates)

[Download the full Xcode project from GitHub.]

Today, I’m going to show you how to leverage the Swift “extension” language feature to manage software complexity, improve code readability, and increase extensibility. We’ll also talk about delegates, data sources, and protocols as they are concepts essential to this tutorial. According to Apple’s “The Swift Programming Language (Swift 3.0.1):”

Continue reading “Using Swift extensions to manage complexity, improve readability, and increase extensibility (UICollectionView, protocols, and delegates)”