ios - 用户界面窗口?没有成员命名边界

标签 ios xcode swift ios8 xcode6

我正在尝试更新 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 协议(protocol)中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

或者,如果你想检查应用程序委托(delegate)的可能性 没有窗口属性,或者它被设置为 nil:

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

} else {
    // report error
}

更新:在 Xcode 6.3 中,delegate 属性现在也是 定义为可选的,所以代码现在是

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?获取更多解决方案。

关于ios - 用户界面窗口?没有成员命名边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25175544/

相关文章:

c# - DisplayAlert (Xamarin.iOS) 内的微调器

swiftui - 如何避免 'Unable to simultaneously satisfy constraints' 错误?

ios - Xcode 7.3.1 在 MacOS Sierra Beta 5 上崩溃

swift - 带有 groupPagingCentered 的 UICollectionViewCompositionalLayout 没有开始居中

ios - 动画期间的帧值错误

ios - 不想按排序顺序对参数进行排序 alamofire

ios - 更新模型 Realm Swift

javascript - 如何结合这两个视频功能?

ios - 使用 xcodebuild 运行单个测试配置

ios - 在不同的 Storyboard、Swift、iOS、Xcode 上通过 segue 和 Navigation 将数据从一个 Controller 传递到另一个 Controller