Last active
June 29, 2017 14:50
-
-
Save dudarenko-io/2211ea627654772ebc4aa00008f46ff6 to your computer and use it in GitHub Desktop.
Swift array flatMap example used to cast array`s content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: CustomStringConvertible { | |
let name: String | |
var residence: String? | |
init(name: String,residence: String) { | |
self.name = name | |
self.residence = residence | |
} | |
var description: String { | |
return "\(type(of:self)) name: \(name)" | |
} | |
} | |
class Programmer: Person {} | |
let ilya: Person = Programmer(name: "Ilya", residence: "Moscow") | |
let vasya: Person = Person(name: "Vasya", residence: "Ryazan") | |
var persons: [Person?] = [ilya, nil, vasya] | |
let programmers = persons.flatMap { (person) -> Programmer? in | |
person as? Programmer | |
} | |
print(programmers) // [Programmer name: Ilya] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment