Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct Abc1 {
var asd = 1
}
struct Abc2 {
var asd = 2
}
struct Abc3 {
var asd = 3
}
class AnyFunction {
func anyIndexDataCheck() {
var sampleAny: [Any] = []
var sampleAny2: [Any] = []
sampleAny = [Abc1(), Abc3(), Abc3(), Abc1(), Abc2(), Abc3()]
for value in sampleAny {
if cast(value: value, type: Abc1.self) != nil || cast(value: value, type: Abc3.self) != nil {
sampleAny2.append(value)
} else {
if let sample = value as? Abc1 {
print(sample.asd, "🟢anyFunctionAbc1")
}
if let sample = value as? Abc2 {
print(sample.asd, "🟢anyFunctionAbc2")
}
if let sample = value as? Abc3 {
print(sample.asd, "🟢anyFunctionAbc3")
}
}
}
print(sampleAny2, "🟢anyFunction")
}
func cast<A>(value: Any?, type: A.Type) -> A? { return value as? A }
}
@daisukenagata
Copy link
Author

daisukenagata commented Jun 15, 2021

スクリーンショット 2021-06-15 9 21 40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment