ios – if-let语句不解包可选

前端之家收集整理的这篇文章主要介绍了ios – if-let语句不解包可选前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的代码中遇到了一些好奇的东西,并且想知道这个行为是否有直接的解释.鉴于以下声明:
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;
                }
            }
        }
    }

据我所知,if-let语句应该为我解开条件 – 但这不是这里展示的行为.除非我再次打开可选项,否则我无法访问subcontrollerView的window属性. x-code控制台返回以下内容

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>>

展开的可选和if-let常量是相同的.为什么?

解决方法

你的问题是AnyObject. (如果有疑问,你的问题总是AnyObject;它是一种应该尽可能避免的邪恶类型.唯一更糟的是AnyObject?)

问题是tabBarController.viewControllers返回[AnyObject] ?,可选的促销可能会导致事情横向发展.它可能会推广AnyObject?一个AnyObject ??然后变得困惑.这有点像编译器错误,但也只是AnyObject带来的疯狂.所以答案就是尽可能快地摆脱它.

而不是这个:

for subcontroller in tabBarController.viewControllers! {

你要这个:

if let viewControllers = tabBarController.viewControllers as? [UIViewController] {
   for subcontroller in viewControllers {

所以完整的代码是这样的:

if let tabBarController = topViewController as? UITabBarController {
    if let viewControllers = tabBarController.viewControllers as? [UIViewController] {
        for subcontroller in viewControllers {
            if let subcontrollerView = subcontroller.view {
                if subcontrollerView.window != nil && subcontroller.isViewLoaded() {
                    topViewController = subcontroller
                    break;
                } } } } }

但我们可以做得更好.首先,可选链接通常是管理多个if-lets的更好方法,当它不能正常工作时,我们可以使用Swift 1.2的新的multi-if-let语法来实现:

if let tabBarController = topViewController as? UITabBarController,viewControllers = tabBarController.viewControllers as? [UIViewController] {
        for subcontroller in viewControllers {
            if subcontroller.view?.window != nil && subcontroller.isViewLoaded() {
                topViewController = subcontroller
                break;
            } } }

猜你在找的iOS相关文章