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 Java

Optional chaining (?.) in nashorn

From Dev

Swift optional chaining doesn't work in closure

From Dev

Swift: NSNumber is not a subtype of UIViewAnimationCurve

From Dev

Swift: NSNumber is not a subtype of Float

From Dev

swift optional chaining with cast

From Dev

Accessing optional values of a Swift dictionary

From Dev

Optional Chaining in JavaScript

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Optional Chaining Not Working As Expected

From Dev

Non-optional var in swift UIViewController subclass

From Dev

Optional Chaining returning an Int

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

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

From Dev

String to NSNumber in Swift

From Dev

Using NSNumber numberWithBool in Swift

From Dev

Optional chaining used in left side of assignment in Swift

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 there something like the swift optional chaining in javascript?

From Dev

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

From Dev

Optional let and optional var inside a class in Swift

From Dev

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

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional chaining with Swift strings

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining and Array in swift

From Dev

Using var to unwrap an optional in Swift?

From Dev

Optional chaining for constructor calls?

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

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

Related Related

  1. 1

    Optional chaining (?.) in nashorn

  2. 2

    Swift optional chaining doesn't work in closure

  3. 3

    Swift: NSNumber is not a subtype of UIViewAnimationCurve

  4. 4

    Swift: NSNumber is not a subtype of Float

  5. 5

    swift optional chaining with cast

  6. 6

    Accessing optional values of a Swift dictionary

  7. 7

    Optional Chaining in JavaScript

  8. 8

    Optional Chaining or ternary expression in Swift?

  9. 9

    Optional Chaining Not Working As Expected

  10. 10

    Non-optional var in swift UIViewController subclass

  11. 11

    Optional Chaining returning an Int

  12. 12

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

  13. 13

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

  14. 14

    String to NSNumber in Swift

  15. 15

    Using NSNumber numberWithBool in Swift

  16. 16

    Optional chaining used in left side of assignment in Swift

  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 there something like the swift optional chaining in javascript?

  19. 19

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

  20. 20

    Optional let and optional var inside a class in Swift

  21. 21

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

  22. 22

    Swift 3.0 Optional Chaining

  23. 23

    Optional chaining with Swift strings

  24. 24

    Chaining Optional.orElseThrow

  25. 25

    Optional chaining and Array in swift

  26. 26

    Using var to unwrap an optional in Swift?

  27. 27

    Optional chaining for constructor calls?

  28. 28

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

  29. 29

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

HotTag

Archive