UIWindow?没有名为bounds的成员。

4

我正在尝试更新PKHUD(https://github.com/pkluz/PKHUD)以使其与Xcode 6 beta 5兼容,并且已经接近完成,只剩一个小问题:

internal class Window: UIWindow {
    required internal init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    internal let frameView: FrameView
    internal init(frameView: FrameView = FrameView()) {
        self.frameView = frameView

        // this is the line that bombs
        super.init(frame: UIApplication.sharedApplication().delegate.window!.bounds)

        rootViewController = WindowRootViewController()
        windowLevel = UIWindowLevelNormal + 1.0
        backgroundColor = UIColor.clearColor()

        addSubview(backgroundView)
        addSubview(frameView)
    }
    // more code here
}

Xcode提示我出错:UIWindow?没有名为'bounds'的成员。 我相信这是与类型转换相关的微不足道的错误,但我已经花费了几个小时无法找到答案。

此外,这个错误仅在Xcode 6 beta 5中发生,这意味着答案在于苹果最近做的修改。

非常感谢您的所有帮助。

1个回答

6

UIApplicationDelegate协议中window属性的声明已更改为

optional var window: UIWindow! { get set } // beta 4

to

optional var window: UIWindow? { get set } // beta 5

这意味着它是一个可选属性,产生一个可选的 UIWindow:

println(UIApplication.sharedApplication().delegate.window)
// Optional(Optional(<UIWindow: 0x7f9a71717fd0; frame = (0 0; 320 568); ... >))

所以你需要将其解包两次:

let bounds = UIApplication.sharedApplication().delegate.window!!.bounds

或者,如果您想检查应用程序委托是否没有窗口属性,或者它被设置为 nil

if let bounds = UIApplication.sharedApplication().delegate.window??.bounds {

} else {
    // report error
}

更新:随着Xcode 6.3的发布,delegate属性也被定义为可选项,因此代码现在应该是这样的。
let bounds = UIApplication.sharedApplication().delegate!.window!!.bounds

或者

if let bounds = UIApplication.sharedApplication().delegate?.window??.bounds {

} else {
    // report error
}

请参见为什么主窗口是双重可选类型,以获取更多解决方案。


你好,你知道为什么从 var window: UIWindow! 改成了 var window: UIWindow 吗?我感觉这没有意义。 - Qiulang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接