Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/cf57c92790dda2d44739d24327aeb2a5 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/cf57c92790dda2d44739d24327aeb2a5 to your computer and use it in GitHub Desktop.
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

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

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