0

I was presenting a controller from a noncontroller class so I grabbed the root view, and got the common error of "not unwrapped" so I put in ? and !'s to try or forcibly unwrap, still said window was not unwrapped, so it auto-fixed it by inserting another.

UIApplication.sharedApplication().delegate?.window!!.rootViewController!.presentViewController(blah blah blah... { () -> Void in
        });

Title says it all. My only guess is the window is basically a computed property that gives an optional, of which you must unwrap it?!? (grammar not a typo, just ensuring I end the sentence without an error)

Qbyte
  • 12,753
  • 4
  • 41
  • 57
Stephen J
  • 2,367
  • 2
  • 25
  • 31
  • Seems the ?? first checks to see if the protocol is implemented, then if it is, unwraps the var. http://stackoverflow.com/questions/29920427/swift-optional-of-optional I have yet to figure out if this should be an answer, comment, or dupe, although the wording and reasons aren't the same. – Stephen J Aug 24 '15 at 17:11

1 Answers1

1

You need two ! because the type is a nested optional (UIWindow??).

Like this:

let nested: Int?? = 3

// the safe way
if let innerValue = nested {
    // innerValue is of type Int?
    if let unwrapped = innerValue {
        // unwrapped is of type Int
    }
}
Qbyte
  • 12,753
  • 4
  • 41
  • 57
  • I don't think it's a nested var inside a var, I couldn't find a reason for that. Eg: what if the outer exists but the inner doesn't. But I did find something that explains how ?? does make sense, seems it's a protocol – Stephen J Aug 24 '15 at 17:09
  • @StephenJ I don't know the reason for the use "double optional" but `Int??` is a shorthand for the TYPE `Optional>`. – Qbyte Aug 24 '15 at 17:42