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

Extending functionality with extensions

Extensions let us add functionality to our existing classes, structs, enums, and protocols. This can be especially useful when the original type is provided by an external framework, which means you aren't able to add functionality directly.

Getting ready

Imagine that we often need to obtain the first word from a given string. Rather than repeatedly writing the code to split the string into words and then retrieving the first word, we can extend the functionality of String to provide its own first word.

How to do it...

Let's get started:

  1. Create an extension of String:
extension String { 

}
  1. Within the extension's curly brackets, add a function that returns the first word from the string:
extension String {
func firstWord() -> String {
let spaceIndex = firstIndex(of: " ") ?? endIndex
let word = prefix(upTo: spaceIndex)
return String(word)
}
}
  1. Now, we can use this new method on String to get the first word from a phrase:
let llap = "Live long, and prosper" 
let firstWord = llap.firstWord()
print(firstWord) // Live

How it works...

We can define an extension using the extension keyword and then specify the type we want to extend. The implementation of this extension is defined within curly brackets:

extension String { 
//...
}

Methods and computed properties can be defined in extensions in the same way that they can be defined within classes, structs, and enums. Here, we will add a firstWord function to the String struct:

extension String {
func firstWord() -> String {
let spaceIndex = firstIndex(of: " ") ?? endIndex
let word = prefix(upTo: spaceIndex)
return String(word)
}
}

The implementation of the firstWord method is not important for this recipe, so we'll just touch on it briefly.

In Swift, String is a collection, so we can use the collection methods to find the first index of an empty space. However, this could be nil since the string may contain only one word or no characters at all, so if the index is nil, we must use the endIndex instead. The nil coalescing operator (??) is only used to assign endIndex if firstIndex(of: " ") is nil. More generally, it will evaluate the value on the left-hand side of the operator, unless it is nil, in which case it will assign the value on the right-hand side.

Then, we use the index of the first space to retrieve the substring up to the index, which has a SubString type. We then use that to create and return a String.

Extensions can implement anything that uses the existing functionality, but they can't store information in a new property. Therefore, computed properties can be added, but stored properties cannot. Let's change our firstWord method so that it's a computed property instead:

extension String {
var firstWord: String {
let spaceIndex = firstIndex(of: " ") ?? endIndex
let word = prefix(upTo: spaceIndex)
return String(word)
}
}

There's more...

Extensions can also be used to add protocol conformance, so let's create a protocol that we want to add conformance to:

  1. The protocol declares that something can be represented as an Int:
protocol IntRepresentable { 
var intValue: Int { get }
}
  1. We can extend Int and have it conform to IntRepresentable by returning itself:
extension Int: IntRepresentable { 
var intValue: Int {
return self
}
}
  1. Next, we'll extend String, and we'll use an Int constructor that takes a String and returns an Int if our String contains digits that represent an integer:
extension String: IntRepresentable { 
var intValue: Int {
return Int(self) ?? 0
}
}
  1. We can also extend our own custom types and add conformance to the same protocol, so let's create an enum that can be IntRepresentable:
enum CrewComplement: Int { 
case enterpriseD = 1014
case voyager = 150
case deepSpaceNine = 2000
}
  1. Since our enum is Int-based, we can conform to IntRepresentable by providing a rawValue:
extension CrewComplement: IntRepresentable { 
var intValue: Int {
return rawValue
}
}
  1. We now have String, Int, and CrewComplement all conforming to IntRepresentable, and since we didn't define String or Int, we have only been able to add conformance through the use of extensions. This common conformance allows us to treat them as the same type:
var intableThings = [IntRepresentable]() 
intableThings.append(55)
intableThings.append(1200)
intableThings.append("5")
intableThings.append("1009")
intableThings.append(CrewComplement.enterpriseD)
intableThings.append(CrewComplement.voyager)
intableThings.append(CrewComplement.deepSpaceNine)

let over1000 = intableThings.compactMap { $0.intValue > 1000 ?
$0.intValue: nil }
print(over1000)

The preceding example includes the use of compactMap and the ternary operator, which haven't been covered in this book. Further information can be found in the See also section.

See also

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

The documentation for compactMap can be found at https://developer.apple.com/documentation/swift/sequence/2950916-compactmap.

Further information about the ternary operator can be found at https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID71.

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