Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active November 2, 2022 22:59
Show Gist options
  • Save carlynorama/330dca5a3172d0e3448ae1808265163c to your computer and use it in GitHub Desktop.
Save carlynorama/330dca5a3172d0e3448ae1808265163c to your computer and use it in GitHub Desktop.
Add this to any Struct if you want a string of all the optional values that actually have defined content.
var whatDoIHave:String {
let mirror = Mirror(reflecting: self)
var itemsToDisplay:[String] = []
for child in mirror.children {
//print("key: \(child.label), value: \(child.value)")
if child.value is ExpressibleByNilLiteral {
let typeDescription = object_getClass(child.value)?.description() ?? ""
if !typeDescription.contains("Null") && !typeDescription.contains("Empty") {
itemsToDisplay.append(child.label ?? "no_key")
}
}
}
let returnString = itemsToDisplay.count > 0 ? "Optionals Available: \(itemsToDisplay.joined(separator: ", "))" : ""
return returnString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment