我正在尝试更新 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? does not have a member named 'bounds'
我很确定这是一个与类型转换相关的微不足道的错误,但几个小时以来我一直无法找到答案。

此外,此错误仅发生在 Xcode 6 beta 5 中,这意味着答案在于 Apple 最近更改的某些内容。

非常感谢所有帮助。

最佳答案

window 协议(protocol)中 UIApplicationDelegate 属性的声明
从改变

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


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

或者,如果您想检查应用程序委托(delegate)的可能性
没有 window 属性,或者它被设置为 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
}

有关更多解决方案,另请参阅 Why is main window of type double optional?

关于ios - 界面窗口?没有成员命名边界,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25175544/

10-10 00:38
查看更多