Last active
June 15, 2021 00:22
-
-
Save DaisukeNagata/cf57c92790dda2d44739d24327aeb2a5 to your computer and use it in GitHub Desktop.
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
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 } | |
} |
Author
DaisukeNagata
commented
Jun 15, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment