Mastering Swift 3 - Linux by Hoffman Jon

Mastering Swift 3 - Linux by Hoffman Jon

Author:Hoffman, Jon
Language: eng
Format: epub, azw3, mobi
Publisher: Packt Publishing
Published: 2017-01-02T00:00:00+00:00


Using the where statement with protocols

When we use a protocol-oriented design, as we just saw, we are able to use the where statement to filter 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 such as this:

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

This will retrieve only those animals that conform to the SeaAnimal protocol. This is a lot safer than just checking the flags because, as we pointed out earlier, it is really easy to set the wrong flag in our code, which would introduce all sorts of weird behavior.



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.