The declaration of the window
property in the UIApplicationDelegate
protocol
changed from
optional var window: UIWindow! { get set } // beta 4
to
optional var window: UIWindow? { get set } // beta 5
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
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
}
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
or
if let bounds = UIApplication.sharedApplication().delegate?.window??.bounds {
} else {
// report error
}
See also Why is main window of type double optional? for more solutions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…