Lets say I have a class that has many properties, and I want to check if most of them are nil...
So, I would like to exclude only two properties from that check (and say, to check against 20 properties).
I tried something like this:
extension MyClass {
func isEmpty() -> Bool {
let excluded = ["propertyName1", "propertyName2"]
let children = Mirror(reflecting: self).children.filter { $0.label != nil }
let filtered = children.filter {!excluded.map{$0}.contains($0.label)}
let result = filtered.allSatisfy{ $0.value == nil }
return result
}
}
The first thing that bothers me about this code is that, I would have to change excluded array values if I change a property name.
But that is less important, and the problem is, this line:
let result = filtered.allSatisfy{ $0.value == nil }
it doesn't really check if a property is nil... Compiler warns about:
Comparing non-optional value of type 'Any' to 'nil' always returns false
So, is there some better / proper way to solve this?