Created
March 19, 2019 20:33
-
-
Save DeepFriedTwinkie/4d015121886dc86a8868fb0c8ae0e68c to your computer and use it in GitHub Desktop.
Filter a mixed value array by type
This file contains 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
protocol Token { | |
var isSomething: Bool {get} | |
} | |
struct BasicToken : Token { | |
let isSomething: Bool | |
} | |
struct FancyToken : Token { | |
let isSomething: Bool | |
let howFancy: Int | |
} | |
let tokens: [Token] = [ | |
BasicToken(isSomething: true), | |
BasicToken(isSomething: false), | |
FancyToken(isSomething: true, howFancy: 3), | |
BasicToken(isSomething: false), | |
FancyToken(isSomething: true, howFancy: 10) | |
] | |
for case let fancy as FancyToken in tokens { | |
print(fancy.howFancy) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment