Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds

I'm working on a keyboard, I just have installed xcode 7 beta 2 and then I am getting lots of Warnings.

Over 24 yellow errors I think it makes the keyboard crash on xcode 6.4 No errors and no keyboard Course

I find difficulty to resolve the errors.

Warnings:

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()
    }
}

Warnings:

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
        }
    }
}
like image 344
Swift2 Avatar asked Dec 14 '25 18:12

Swift2


1 Answers

These aren't errors, they are just warnings and they probably aren't the cause of your crashes however you can resolve these two examples by doing the following:

The UITextDocumentProxy protocol conforms to UIKeyInput anyway so there is no need to cast kbd.textDocumentProxy as UIKeyInput.

You will be able to do the following without any warnings:

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()
    }
}

Same with obj, compiler already knows that it is a UITouch object so there is no need to cast it, you can take all the code out of the if let touch = obj as? UITouch statement like so:

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
    }
}

Little tip: alt click on a variable to see what type it has been resolved as:

enter image description here

like image 95
liamnichols Avatar answered Dec 16 '25 12:12

liamnichols



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!