的UIWindow?没有名为bounds的成员 [英] UIWindow? does not have member named bounds
问题描述
我正在尝试更新PKHUD( https://github.com/pkluz/PKHUD )使用Xcode 6 beta 5几乎可以使用除了一个小细节:
I'm trying to update PKHUD (https://github.com/pkluz/PKHUD) to work with Xcode 6 beta 5 and am almost through except for one tiny detail:
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
}
Xc颂歌给我错误 UIWindow?没有名为'bounds'的成员
。
我很确定这是一个与打字有关的微不足道的错误,但我几个小时都找不到答案。
Xcode gives me the error UIWindow? does not have a member named 'bounds'
.
I'm pretty sure this is a trivial mistake related to type-casting, but I've been unable to find the answer to this for a couple hours.
此外,此错误仅发生在Xcode 6 beta 5中,这意味着答案在于Apple最近更改的内容。
Also, this error occurs only in Xcode 6 beta 5, which means that the answer lies in something Apple changed recently.
非常感谢所有帮助。
推荐答案
UIApplicationDelegate中
协议窗口
属性的声明
从
The declaration of the window
property in the UIApplicationDelegate
protocol
changed from
optional var window: UIWindow! { get set } // beta 4
到
optional var window: UIWindow? { get set } // beta 5
这意味着它是一个可选属性,产生一个可选的 UIWindow
:
which means that it is an optional property yielding an optional UIWindow
:
println(UIApplication.sharedApplication().delegate.window)
// Optional(Optional(<UIWindow: 0x7f9a71717fd0; frame = (0 0; 320 568); ... >))
所以你必须打开它两次:
So you have to unwrap it twice:
let bounds = UIApplication.sharedApplication().delegate.window!!.bounds
或者,如果你想检查是否有可能应用程序委托有
无窗口属性,或者设置为 nil
:
or, if you want to check for the possibility that the application delegate has
no window property, or it is set to nil
:
if let bounds = UIApplication.sharedApplication().delegate.window??.bounds {
} else {
// report error
}
更新:使用Xcode 6.3,委托
属性现在也是
被定义为可选,所以代码现在是
Update: With Xcode 6.3, the delegate
property is now also
defined as an optional, so the code would now be
let bounds = UIApplication.sharedApplication().delegate!.window!!.bounds
或
if let bounds = UIApplication.sharedApplication().delegate?.window??.bounds {
} else {
// report error
}
另见为什么主窗口是输入double optional?以获得更多解决方案。
See also Why is main window of type double optional? for more solutions.
这篇关于的UIWindow?没有名为bounds的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!