Accessing boolValue in a NSNumber var with optional chaining (in Swift) -


i have nsmanagedobject subclass optional instance variable

@nsmanaged var condition: nsnumber? // refers optional boolean value in data model 

i'd when condition variable exists , contains 'true'.

of course, can this:

if let cond = condition {  if cond.boolvalue {   //  } } 

however, hoped possible same thing little bit more compact optional chaining. this:

if condition?.boolvalue {   // } 

but produces compiler error:

optional type '$t4??' cannot used boolean; test '!= nil' instead

the compact way solve problem this:

if condition != nil && condition!.boolvalue {  // } 

is there no way access boolean value optional chaining, or missing here?

you can compare boolean value:

if condition == true {     ... } 

some test cases:

var testzero: nsnumber? = 0 var testone: nsnumber? = 1 var testtrue: nsnumber? = true var testnil: nsnumber? = nil var testinteger: nsnumber? = 10  if testzero == true {     // not true }  if testone == true {     // it's true }  if testtrue == true {     // it's true }  if testnil == true {     // not true }  if testinteger == true {     // not true } 

the interesting thing 1 recognized true - expected, because type nsnumber


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -