关于ios:yellow警告:从UITextDocumentProxy强制转换为UIKeyInput的条件转换始终成功

yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

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

我认为超过24个黄色错误会导致键盘崩溃
在xcode 6.4上没有错误,没有键盘课程

我发现很难解决这些错误。

警告:

Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

您将能够执行以下操作而不会发出任何警告:

1
2
3
4
5
6
7
8
9
10
11
12
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语句中删除:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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单击变量以查看其解析为的类型:

enter image description here