Why is main window of type double optional?

前端 未结 4 1978
独厮守ぢ
独厮守ぢ 2020-11-29 05:09

When accessing UIapplication\'s main window it is returned as a UIWindow??

let view = UIApplication.sharedApplication().delegate?.w         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 05:38

    Oh the double optional! Sometimes you can use a double-bang (two exclamation marks) but you cannot cast that way with optional binding. So... my remix of all the other code gets you a UIWindow object called window of the non-optional kind:

    guard let w = UIApplication.shared.delegate?.window, let window = w else { return }
    

    But let's not waste time and just use

    let window = UIApplication.shared.delegate!.window!!
    

    and be done.

提交回复
热议问题