Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Swift Protocol-Oriented Programming - Fourth Edition

You're reading from  Swift Protocol-Oriented Programming - Fourth Edition

Product type Book
Published in Jun 2019
Publisher
ISBN-13 9781789349023
Pages 224 pages
Edition 4th Edition
Languages
Author (1):
Jon Hoffman Jon Hoffman
Profile icon Jon Hoffman

Table of Contents (11) Chapters

Preface 1. Starting with the Protocol 2. Our Type Choices 3. Extensions 4. Generics 5. Memory Management 6. Object-Oriented Programming 7. Protocol-Oriented Programming 8. Adopting Design Patterns in Swift 9. Case Studies 10. Other Books You May Enjoy

Type casting with protocols

Type casting is a way to check the type of an instance and/or to treat the instance as a specified type. In Swift, we use the is keyword to check whether an instance is of a specific type and the as keyword to treat an instance as a specific type.

The following example shows how we would use the is keyword:

if person is SwiftProgrammer { 
    print("(person.firstName) is a Swift Programmer") 
} 

In this example, the conditional statement returns true if the Person instance is of the SwiftProgrammer type, or false if it isn't. We can use the where statement in combination with the is keyword to filter an array to only return instances of a specific type. In the following example, we filter an array that contains instances of the Person protocol and have it only return those elements of the array that are instances of the SwiftProgrammer type:

for person in people where person is SwiftProgrammer { 
    print("(person.firstName) is a Swift Programmer") 
}  

Now, let's look at how we would cast an instance to a specific type. To do this, we can use the as keyword. Since the cast can fail if the instance is not of the specified type, the as keyword comes in two forms: as? and as!. With the as? form, if the casting fails, it returns a nil. With the as! form, if the casting fails, a runtime error is thrown; therefore, it is recommended to use the as? form unless we are absolutely sure of the instance type or if we perform a check of the instance type prior to doing the cast. The following example shows how we would use the as? keyword to attempt to cast an instance of a variable to the SwiftProgammer type:

if let _ = person as? SwiftProgrammer { 
    print("(person.firstName) is a Swift Programmer") 
} 

Since the as? keyword returns an optional, we could use optional binding to perform the cast.

Now, let's see how we can use associated types with protocols.

You have been reading a chapter from
Swift Protocol-Oriented Programming - Fourth Edition
Published in: Jun 2019 Publisher: ISBN-13: 9781789349023
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}