ios - 黄色警告 : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

标签 ios xcode swift xcode7 ios9

我正在使用键盘,我刚刚安装了 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 协议(protocol)都符合 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对象,所以不需要强制转换,你可以把所有的代码都从中取出来>如果让 touch = obj 作为? 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 点击一个变量来查看它被解析为什么类型:

enter image description here

关于ios - 黄色警告 : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262500/

相关文章:

swift - 动画后无法隐藏 View

ios - CGImageRef 消耗大量内存

ios - 难以更改 UITableViewCell 中图像的大小

Swift 脚本直接在 Xcode Run Script 阶段运行,但由于 NSTask/Process 而不能通过外部文件运行

iphone - <加速/加速.h> "file not found"

ios - Xcode 7/Swift : Save value of slider and send to child class when view changes

ios - CGAffineTransformMakeRotation 不调整 iOS 6 中的 View 大小

ios - 评估类的方法或属性

iphone - 如何从我的应用程序包中读取文本文件并将其显示在 UITextView 中

ios - 将可选 TextView 的顶部 anchor 限制为底部 anchor