我正在尝试更新 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 中,这意味着答案在于 Apple 最近更改的内容。
非常感谢所有帮助。
最佳答案
UIApplicationDelegate
协议(protocol)中window
属性的声明
改自
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)的可能性
没有窗口属性,或者它被设置为 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/