Optional Chaining Not Working As Expected

fae53351b9effc708e764e871bef31

I have the following code in my UIViewController subclass

class SideMenu: UIViewController {

  var contentViewController: UIViewController?

   override func shouldAutorotate() -> Bool {

        return contentViewController?.shouldAutorotate()

    }
}

But for some reason I get the following error:

Value of optional type 'Bool?' not unwrapped; did you mean to use '!' or '??'

Screenshot of above error message on the return line

I would expect that the optional chaining unwraps the optional, but this dosen't seem to be true? Am I wrong?

Brian Nickel

The result of an optional chain is optional. So ?.shouldAutorotate() yields a Bool? while your function expects a Bool. Hence the error:

Value of optional type 'Bool?' not unwrapped; did you mean to use '!' or '??'

There are two possible solutions outlined by the error. One is to unwrap either with contentViewController!.shouldAutorotate() or contentViewController?.shouldAutorotate()! but both of these will crash if contentViewController was nil and that's not what you had in mind.

The other option is to provide a fallback value in case your Bool? is nil. There's a nice operator for chaining: ?? which takes T? on the lefthand side and T on the righthand side.

That is, if you want to return false when contentViewController is nil you would return the following:

return contentViewController?.shouldAutorotate() ?? false

This is effectively the same behavior as the following code:

if let controller = contentViewController {
    return controller.shouldAutorotate()
} else { 
    return false
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ContinueWith chaining not working as expected

From Dev

Optional chaining not working for optional protocol requirements

From Dev

Symfony Optional constraints not working as expected

From Java

Typescript optional chaining error: Expression expected.ts(1109)

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional Chaining in JavaScript

From Dev

swift optional chaining with cast

From Dev

Optional chaining with Swift strings

From Dev

Chaining Optional.orElseThrow

From Dev

Optional chaining and Array in swift

From Dev

Optional chaining for constructor calls?

From Dev

Is there an equivalent to optional chaining with arithmetic operators?

From Dev

dynamicType of optional chaining not the same as assignment

From Dev

Optional.ofNullable and method chaining

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

.on("hover", ...) chaining not working

From Dev

Chaining not working on jQuery Plugin

From Dev

CABasicAnimation chaining not working

From Dev

chaining ifelse with mutate not working

From Dev

Swift optional chaining doesn't work in closure

From Dev

Java8 Optional with Function chaining expression

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

How to know where Optional Chaining is breaking?

From Dev

Optional chaining question mark after function name

From Java

How to use optional chaining with array in Typescript?

From Dev

how to extend optional chaining for all types

Related Related

HotTag

Archive