Swift2
Swift2

Reputation: 169

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

Upvotes: 3

Views: 3005

Answers (1)

Liam
Liam

Reputation: 12668

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

Upvotes: 2

Related Questions