protocol-extension

Swift make protocol extension a Notification observer

99封情书 提交于 2019-11-29 21:30:25
Let's consider the following code: protocol A { func doA() } extension A { func registerForNotification() { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil) } func keyboardDidShow(notification: NSNotification) { } } Now look at a UIViewController subclass that implements A: class AController: UIViewController, A { override func viewDidLoad() { super.viewDidLoad() self.registerForNotification() triggerKeyboard() } func triggerKeyboard() { // Some code that make key board appear } func doA() { } } But

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

我们两清 提交于 2019-11-29 07:47:49
I wrote an extension onto Int as below. extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works The above code works. Now I want to extend the IntegerType protocol so that Int, UInt, Int64, etc would all conform. My code is as below. extension IntegerType { func squared () -> IntegerType { // this line creates error return self * self } } I get error: Protocol 'IntegerType' can only be used as a generic constraint because it has Self or associated type requirements I already saw this question and its video & this question, still couldn't understand. I only

Swift protocol extension method dispatch with superclass and subclass

你。 提交于 2019-11-29 07:05:42
I found an interesting behaviour which seems like a bug... Based on the behaviour described the following articles: https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94 http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future The output is not what I expect, when I add SomeSuperclass , rather than directly adopting the protocol. protocol TheProtocol { func method1() } extension TheProtocol { func method1() { print("Called method1 from protocol extension") } func method2NotInProtocol() { print("Called method2NotInProtocol from protocol extension") } }

Extend existing protocols to implement another protocol with default implements

假如想象 提交于 2019-11-29 05:41:40
问题 Is it possible to add protocol compliance to a different protocol by way of an extension? For instance we would like A to comply with B: protocol A { var a : UIView {get} } protocol B { var b : UIView {get} } I want to give a default implementation (compliance) of B to objects of type A // This isn't possible extension A : B { var b : UIView { return self.a } } The motivation being to reuse objects of A in cases where a B is required without creating my own "bridge" class MyClass { func

Swift make protocol extension a Notification observer

泪湿孤枕 提交于 2019-11-28 19:01:50
问题 Let's consider the following code: protocol A { func doA() } extension A { func registerForNotification() { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil) } func keyboardDidShow(notification: NSNotification) { } } Now look at a UIViewController subclass that implements A: class AController: UIViewController, A { override func viewDidLoad() { super.viewDidLoad() self.registerForNotification()

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

荒凉一梦 提交于 2019-11-28 01:36:26
问题 I wrote an extension onto Int as below. extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works The above code works. Now I want to extend the IntegerType protocol so that Int, UInt, Int64, etc would all conform. My code is as below. extension IntegerType { func squared () -> IntegerType { // this line creates error return self * self } } I get error: Protocol 'IntegerType' can only be used as a generic constraint because it has Self or associated type