加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – UIWindow?没有名为bounds的成员

发布时间:2020-12-15 01:46:51 所属栏目:百科 来源:网络整理
导读:我正在尝试更新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 frame
我正在尝试更新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协议中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

或者,如果您想检查应用程序代表的可能性
没有窗口属性,或者设置为nil:

if let bounds = UIApplication.sharedApplication().delegate.window??.bounds {

} else {
    // report error
}

更新:使用Xcode 6.3,委托属性现在也是
定义为可选,所以代码现在是

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?.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读