Created
June 1, 2022 14:59
-
-
Save NikolajMosbaek/4b2ec644d719a82b7a525148ff3bd8b4 to your computer and use it in GitHub Desktop.
This code will try to match a (search) string to any of an object's string attributes
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
func ~=(lhs: Any, rhs: String) -> Bool { | |
let mirror = Mirror(reflecting: lhs) | |
for child in mirror.children { | |
guard let value = child.value as? String else { continue } | |
if value.localizedCaseInsensitiveContains(rhs) { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From HWS+ https://www.hackingwithswift.com/plus/advanced-swift/runtime-reflection-with-mirror