I have a subclass of NSManagedObject with an optional instance variable
@NSManaged var condition: NSNumber?
I would like to do something when the condition variable exists and contains "true".
Of course, I can do it like this:
if let cond = condition { if cond.boolValue { // do something } }
However, I was hoping that it would be possible to do the same a little more compactly with the optional chain. Something like that:
if condition?.boolValue { // do something }
But this causes a compiler error:
Additional type '$ T4 ??' cannot be used as logical; test instead of '! = nil'
The most compact way to solve this problem:
if condition != nil && condition!.boolValue { // do something }
Is there no way to access a boolean with an optional chain, or am I missing something here?
swift optional nsnumber
Zaggo
source share