Mastering Swift 4 by Jon Hoffman

Mastering Swift 4 by Jon Hoffman

Author:Jon Hoffman
Language: eng
Format: epub
Tags: COM051010 - COMPUTERS / Programming Languages / General, COM046020 - COMPUTERS / Operating Systems / Macintosh, COM051460 - COMPUTERS / Programming / Mobile Devices
Publisher: Packt Publishing
Published: 2017-09-26T11:40:07+00:00


Using the where statement with protocols

With protocols, we are able to use the where statement to filter the instances of our types. For example, if we only want to get the instances that conform to the SeaAnimal protocol, we can create a for loop as follows:

for (index, animal) in animals.enumerated() where animal is SeaAnimal { print("Only Sea Animal: \(index)") }

This will retrieve only the animals that conform to the SeaAnimal protocol. This is a lot safer than using flags as we did in the object-oriented design.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.