Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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