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

Zaggo

I have a NSManagedObject subclass with an optional instance variable

@NSManaged var condition: NSNumber? // This refers to a optional boolean value in the data model

I'd 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 hoped it would be possible to do the same thing a little bit more compact with optional chaining. Something like this:

if condition?.boolValue {
  // do something
}

But this produces a compiler error:

Optional type '$T4??' cannot be used as a boolean; test for '!= nil' instead

The most compact way to solve this problem was this:

if condition != nil && condition!.boolValue {
 // do something
}

Is there really no way to access the boolean value with optional chaining, or am I missing something here?

Antonio

You can just compare it to a 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 most interesting thing is that 1 is recognized as true - which is expected, because the type is NSNumber

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift 3.0 Optional Chaining

From Dev

swift optional chaining with cast

From Dev

Optional chaining with Swift strings

From Dev

Optional chaining and Array in swift

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Swift optional chaining doesn't work in closure

From Dev

Is there something like the swift optional chaining in javascript?

From Dev

Optional chaining used in left side of assignment in Swift

From Dev

Accessing optional values of a Swift dictionary

From Dev

Optional let and optional var inside a class in Swift

From Dev

Using var to unwrap an optional in Swift?

From Dev

How to use optional chaining while searching through a dictionary in swift?

From Dev

Is Swift optional chaining lazily evaluated left-to-right?

From Dev

optional chaining in Swift 3: why does one example work and not the other?

From Dev

How to convert Swift optional NSNumber to optional Int? (any improvements on my code?)

From Dev

Non-optional var in swift UIViewController subclass

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Optional Chaining in JavaScript

From Dev

Optional Chaining Not Working As Expected

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining for constructor calls?

From Dev

Swift: NSNumber is not a subtype of UIViewAnimationCurve

From Dev

Swift: NSNumber is not a subtype of Float

From Dev

Using NSNumber numberWithBool in Swift

From Dev

String to NSNumber in Swift

Related Related

  1. 1

    Swift 3.0 Optional Chaining

  2. 2

    swift optional chaining with cast

  3. 3

    Optional chaining with Swift strings

  4. 4

    Optional chaining and Array in swift

  5. 5

    Optional Chaining or ternary expression in Swift?

  6. 6

    Swift optional chaining doesn't work in closure

  7. 7

    Is there something like the swift optional chaining in javascript?

  8. 8

    Optional chaining used in left side of assignment in Swift

  9. 9

    Accessing optional values of a Swift dictionary

  10. 10

    Optional let and optional var inside a class in Swift

  11. 11

    Using var to unwrap an optional in Swift?

  12. 12

    How to use optional chaining while searching through a dictionary in swift?

  13. 13

    Is Swift optional chaining lazily evaluated left-to-right?

  14. 14

    optional chaining in Swift 3: why does one example work and not the other?

  15. 15

    How to convert Swift optional NSNumber to optional Int? (any improvements on my code?)

  16. 16

    Non-optional var in swift UIViewController subclass

  17. 17

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  18. 18

    Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

  19. 19

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  20. 20

    Optional chaining (?.) in nashorn

  21. 21

    Optional Chaining returning an Int

  22. 22

    Optional Chaining in JavaScript

  23. 23

    Optional Chaining Not Working As Expected

  24. 24

    Chaining Optional.orElseThrow

  25. 25

    Optional chaining for constructor calls?

  26. 26

    Swift: NSNumber is not a subtype of UIViewAnimationCurve

  27. 27

    Swift: NSNumber is not a subtype of Float

  28. 28

    Using NSNumber numberWithBool in Swift

  29. 29

    String to NSNumber in Swift

HotTag

Archive