黄色警告:从 UITextDocumentProxy 到 UIKeyInput 的条件转换总是成功

yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

我正在使用键盘,我刚刚安装了 xcode 7 beta 2 然后我收到很多警告。

超过 24 个黄色错误我认为这会使键盘崩溃 on xcode 6.4 没有错误也没有键盘课程

我很难解决这些错误。

警告:

Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

func handleBtnPress(sender: UIButton) {
    if let kbd = self.keyboard {
        if let textDocumentProxy = kbd.textDocumentProxy as? UIKeyInput {
            textDocumentProxy.insertText(sender.titleLabel!.text!)
        }

        kbd.hideLongPress()

        if kbd.shiftState == ShiftState.Enabled {
            kbd.shiftState = ShiftState.Disabled
        }

        kbd.setCapsIfNeeded()
    }
}

警告:

Conditional cast from UITouch to UITouch always succeeds

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for obj in touches {
        if let touch = obj as? UITouch {
            let view = self.touchToView[touch]

            let touchPosition = touch.locationInView(self)

            if self.bounds.contains(touchPosition) {
                self.handleControl(view, controlEvent: .TouchUpInside)
            }
            else {
                self.handleControl(view, controlEvent: .TouchCancel)
            }

            self.touchToView[touch] = nil
        }
    }
}

这些不是错误,它们只是警告,它们可能不是导致崩溃的原因,但是您可以通过执行以下操作解决这两个示例:

UITextDocumentProxy 协议无论如何都符合 UIKeyInput,因此无需将 kbd.textDocumentProxy 转换为 UIKeyInput

您将能够在没有任何警告的情况下执行以下操作:

func handleBtnPress(sender: UIButton) {
    if let kbd = self.keyboard {
        kbd.textDocumentProxy.insertText(sender.titleLabel!.text!)
        kbd.hideLongPress()

        if kbd.shiftState == ShiftState.Enabled {
            kbd.shiftState = ShiftState.Disabled
        }

        kbd.setCapsIfNeeded()
    }
}

obj一样,编译器已经知道它是一个UITouch对象,所以不需要转换它,你可以把所有代码都从if let touch = obj as? UITouch语句中取出来像这样:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let view = self.touchToView[touch]

        let touchPosition = touch.locationInView(self)

        if self.bounds.contains(touchPosition) {
            self.handleControl(view, controlEvent: .TouchUpInside)
        }
        else {
            self.handleControl(view, controlEvent: .TouchCancel)
        }

        self.touchToView[touch] = nil
    }
}

小提示:alt点击一个变量可以看到它被解析为什么类型: