我正在尝试更新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'。我敢肯定这是与类型转换有关的小错误,但是我已经几个小时找不到答案了。
UIWindow? does not have a member named 'bounds'
另外,此错误仅在Xcode 6 beta 5中发生,这意味着答案在于Apple最近更改的内容。
非常感谢所有帮助。
协议中的window属性声明UIApplicationDelegate从
window
UIApplicationDelegate
optional var window: UIWindow! { get set } // beta 4
至
optional var window: UIWindow? { get set } // beta 5
这意味着它是一个可选属性,产生一个可选UIWindow:
UIWindow
println(UIApplication.sharedApplication().delegate.window) // Optional(Optional(<UIWindow: 0x7f9a71717fd0; frame = (0 0; 320 568); ... >))
因此,您必须将其拆开两次:
let bounds = UIApplication.sharedApplication().delegate.window!!.bounds
或者,如果您要检查应用程序委托没有window属性的可能性,或者将其设置为nil:
nil
if let bounds = UIApplication.sharedApplication().delegate.window??.bounds { } else { // report error }
更新: 使用Xcode 6.3,该delegate属性现在也被定义为可选属性,因此该代码现在为
delegate
let bounds = UIApplication.sharedApplication().delegate!.window!!.bounds
要么
if let bounds = UIApplication.sharedApplication().delegate?.window??.bounds { } else { // report error }