Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 2.0 - UITextFieldDelegate protocol extension not working

Tags:

I'm trying to add a default behavior on some UITextFieldDelegate's methods using protocol extensions like so :

extension ViewController: UITextFieldDelegate {
    // Works if I uncommented this so I know delegates are properly set
//    func textFieldShouldReturn(textField: UITextField) -> Bool {
//        textField.resignFirstResponder()
//        return true
//    }
}

extension UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        return true
    }
}

As you may guess, the keyboard never dismiss. I can't really see where's the problem here. Is this a language limitation ? Did someone already success doing it ?

EDIT :

As @Logan suggested, default protocol's method implementation doesn't work with protocols marked as @objc. However, UITextFieldDelegate has the following signature public protocol UITextFieldDelegate : NSObjectProtocol {...}

I've test default implementation for NSObjectProtocol and it seems to works fine :

protocol Toto: NSObjectProtocol {
    func randomInt() -> Int
}

extension Toto {
    func randomInt() -> Int {
        return 0
    }
}

class Tata: NSObject, Toto {}

let int = Tata().randomInt() // returns 0
like image 727
Yaman Avatar asked Sep 12 '15 18:09

Yaman


1 Answers

I can't be 100% positive, but here's what I believe is happening:

Protocol extensions aren't accessible from ObjC. Since UITextFieldDelegate is an ObjC protocol, its reliant on ObjC dispatching. As far as the compiler is concerned, the methods in your default implementation are inaccessible, even though they do exist.

To clarify, we can extend these protocols if its truly an extension and adds behavior. This behavior will only be accessible in Swift and shouldn't be problematic in any way.

The problem is default implementations not being ObjC accessible.

Here's a quick example of a custom version:

@objc protocol Test : class {
    func someFunc() -> String
}

extension Test {
    func someFunc() -> String {
        return ""
    }
}

// Fails here 'candidate is not @objc but protocol requires it`
class Hi : NSObject, Test {

}

Xcode suggests appending @objc but it will keep suggesting this over and over again until you get @objc @objc @objc Hi : ...

Based on our conversation below, I made this which seems to be working. I can't fully explain why yet:

@objc public protocol Toto: UITextFieldDelegate {
    optional func randomInt() -> Int
}

extension Toto {
    func randomInt() -> Int {
        return 0
    }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        return false
    }
}

class Tata: NSObject, Toto {
}

Ok, I realize that I'm considering a different problem, and while this compiles, it won't work, and the issue is dynamic dispatch. If you try to append your method w/ @objc, or dynamic, the compiler will warn you that you can't dispatch this way, except on classes. Since a protocol exception doesn't conform to this, when ObjC dispatches the message send, it can't find the implementation in your extension.

Since Swift is constantly updating, here's when this answer was applicable:

Swift 2.0 Xcode 7 GM

like image 123
Logan Avatar answered Feb 16 '23 01:02

Logan