Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Swift Cookbook
  • Toc
  • feedback
Swift Cookbook

Swift Cookbook

By : Keith Moon, D. Moon, Chris Barker
5 (10)
close
Swift Cookbook

Swift Cookbook

5 (10)
By: Keith Moon, D. Moon, Chris Barker

Overview of this book

Swift is an exciting, multi-platform, general-purpose programming language, and with this book, you'll explore the features of its latest version, Swift 5.3. The book begins with an introduction to the basic building blocks of Swift 5.3, its syntax, and the functionalities of Swift constructs. You’ll then discover how Swift Playgrounds provide an ideal platform to write, execute, and debug your Swift code. As you advance through the chapters, the book will show you how to bundle variables into tuples or sets, order your data with an array, store key-value pairs with dictionaries, and use property observers. You’ll also get to grips with the decision-making and control structures in Swift, examine advanced features such as generics and operators, and explore functionalities outside of the standard library. Once you’ve learned how to build iOS applications using UIKit, you'll find out how to use Swift for server-side programming, run Swift on Linux, and investigate Vapor. Finally, you'll discover some of the newest features of Swift 5.3 using SwiftUI and Combine to build adaptive and reactive applications, and find out how to use Swift to build and integrate machine learning models along with Apple’s Vision Framework. By the end of this Swift book, you'll have discovered solutions to boost your productivity while developing code using Swift 5.3.
Table of Contents (14 chapters)
close
12
About Packt

Getting property changing notifications using property observers

It's common to want to know when a property's value changes. Perhaps you want to update the value of another property or update some user interface element. In Objective-C, this was often accomplished by writing your own getter and setter or using Key-Value Observing (KVO), but in Swift, we have native support for property observers.

Getting ready

To examine property observers, we should create an object with a property that we want to observe. Let's create an object that manages users and a property that holds the current user's name:

class UserManager { 
var currentUserName: String = "Emmanuel Goldstein"
}

We want to present some friendly messages when the current user changes. We'll use property observers to do this.

How to do it...

Let's get started:

  1. Amend the currentUserName property definition so that it looks as follows:
class UserManager { 
var currentUserName: String = "Emmanuel Goldstein" {
willSet (newUserName) {
print("Goodbye to \(currentUserName)")
print("I hear \(newUserName) is on their way!")
}
didSet (oldUserName) {
print("Welcome to \(currentUserName)")
print("I miss \(oldUserName) already!")
}
}
}
  1. Create an instance of UserManager and change the current username. This will generate friendly messages:
let manager = UserManager() 

manager.currentUserName = "Dade Murphy"
// Goodbye to Emmanuel Goldstein
// I hear Dade Murphy is on their way!
// Welcome to Dade Murphy
// I miss Emmanuel Goldstein already!

manager.currentUserName = "Kate Libby"
// Goodbye to Dade Murphy
// I hear Kate Libby is on their way!
// Welcome to Kate Libby
// I miss Dade Murphy already!

How it works...

Property observers can be added within curly brackets after the property declaration, and there are two types: willSet and didSet.

The willSet observer will be called before the property is set and provides the value that will be set on the property. This new value can be given a name within brackets; for example, newUserName:

willSet (newUserName) { 
//...
}

The didSet observer will be called after the property is set and provides the value that the property had before being set. This old value can be given a name within brackets; for example, oldUserName:

didSet (oldUserName) { 
//...
}

There's more...

The new value and old value that are passed into the property observers have implicit names, so there is no need to explicitly name them. The willSet observer is passed a value with an implicit name of newValue, and the didSet observer is passed a value with an implicit name of oldValue.

Therefore, we can remove our explicit names and use the implicit value names:

class UserManager { 
var currentUserName: String = "Emmanuel Goldstein" {
willSet {
print("Goodbye to \(currentUserName)")
print("I hear \(newValue) is on their way!")
}
didSet {
print("Welcome to \(currentUserName)")
print("I miss \(oldValue) already!")
}
}
}

See also

Further information about property observers can be found in Apple's documentation on the Swift language at http://swiftbook.link/docs/properties.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete