I've run across something in my code that seems curious and was wondering if there is a straightforward explanation for this behavior. Given the following statement:
if let tabBarController = topViewController as? UITabBarController {
for subcontroller in tabBarController.viewControllers! {
println(subcontroller.view)
if let subcontrollerView = subcontroller.view {
println(subcontrollerView)
println(subcontrollerView!)
if subcontrollerView!.window != nil && subcontroller.isViewLoaded() {
topViewController = subcontroller as? UIViewController
break;
}
}
}
}
Now as far as I know the if-let statement should unwrap the conditional for me--but this is not the behavior exhibited here. I cannot access the window
property of subcontrollerView
unless I unwrap the optional again. The x-code console returns the following:
Optional(<UILayoutContainerView: 0x7fbccd44e7f0; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x7fbccacdde90>; layer = <CALayer: 0x7fbccd440e30>>)
Optional(<UILayoutContainerView: 0x7fbccd44e7f0; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x7fbccacdde90>; layer = <CALayer: 0x7fbccd440e30>>)
<UILayoutContainerView: 0x7fbccd44e7f0; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x7fbccacdde90>; layer = <CALayer: 0x7fbccd440e30>>
The unwrapped optional and the if-let constant are the same. Why?