Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Nate Birkholz via swift-users
That works great and is very elegant. Nice work. On Sun, Jun 4, 2017 at 6:34 AM, Geordie J wrote: > In fact, this would be even better (avoids unnecessary implicitly > unwrapped optionals): > > class ClosureGestureRecognizer { > fileprivate var recognizer:

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Charles Srstka via swift-users
Here’s what I’d try. (disclaimer: written in Mail, may contain errors, yadda yadda yadda) public class BlockTapGestureRecognizer: UITapGestureRecognizer { private class Target: NSObject { private let closure: (UITapGestureRecognizer) -> () init(closure: @escaping

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Nate Birkholz via swift-users
Technically it does work(!), but I am hesitant to use it in what amounts to sample code for my current job search, haha. On Sun, Jun 4, 2017 at 4:55 AM, Zhao Xin wrote: > Will this work? > > class TapGestureRecognizer: UITapGestureRecognizer { > > var onTap: (() -> Void)?

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Geordie J via swift-users
In fact, this would be even better (avoids unnecessary implicitly unwrapped optionals): class ClosureGestureRecognizer { fileprivate var recognizer: GestureRecognizer private var onAction: ((GestureRecognizer) -> Void) init(onAction: @escaping ((GestureRecognizer) -> Void)) {

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Geordie J via swift-users
To get around the issue of using self on init, but also that of multiple recogniser types, try this: class ClosureGestureRecognizer { // These are initially nil and set on init to their desired values. // This gets around the issue of using self in init. // `private` means they can't

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Zhao Xin via swift-users
Will this work? class TapGestureRecognizer: UITapGestureRecognizer { var onTap: (() -> Void)? init(onTap: (() -> Void)?) { self.onTap = onTap super.init(target: nil, action: nil) *self.removeTarget(nil, action: nil)* self.addTarget(self, action:

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Nate Birkholz via swift-users
Also, note that I tried the following: class BlockTapGestureRecognizer: UIGestureRecognizer { var onTap: (() -> Void)? init(onTap: (() -> Void)?) { self.onTap = onTap super.init(target: nil, action: nil) self.addTarget(self, action: #selector(internalTapHandler))

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Nate Birkholz via swift-users
Ah, I didn't read the rest of the thread. As Zhao Xin notes, you cannot call super.init(target: self, action: #selector()), and you cannot call super.init() in a subclass init or convenience init, it *has* to be the designated init method init(target:action:). That's too bad, closure-based gesture

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-04 Thread Nate Birkholz via swift-users
I briefly considered something like this but didn't explore it. Elegant. Sent from my iPhone, please excuse brevity and errors > On Jun 3, 2017, at 9:38 PM, Geordie Jay wrote: > > I am dealing with a variant of this on Android right now. I have just > subclassed e.g.

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Geordie Jay via swift-users
That's why I thanked for for the amendment. As I said, typing code blindly on the phone, mistakes are inevitable. Thanks for clearing it up. On Sun 4. Jun 2017 at 15:04, Zhao Xin wrote: > I was not talking about the formatting. I am talking about the > implementation. > > You

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Zhao Xin via swift-users
I was not talking about the formatting. I am talking about the implementation. You can't use `self` before you call `super.init` as you did now. If changing your implementation to called `super.init` and then call `self` in `super.init` again. You would have called `super.init` twice. I don't

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Geordie Jay via swift-users
Thanks for the amendment, and sorry for the (lack of) formatting. I painstakingly typed that on my phone with manually-spaced indenting, which the Inbox app unhelpfully removed entirely when I pressed send. Pasting into Xcode should do the trick though.. Geordie On Sun 4. Jun 2017 at 14:49, Zhao

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Zhao Xin via swift-users
You should change to another way. Using `self` in `super.init` is not allowed. Zhaoxin On Sun, Jun 4, 2017 at 12:38 PM, Geordie Jay wrote: > I am dealing with a variant of this on Android right now. I have just > subclassed e.g. UITapGestureRecognizer to perform the 2nd

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Geordie Jay via swift-users
I am dealing with a variant of this on Android right now. I have just subclassed e.g. UITapGestureRecognizer to perform the 2nd variant above and externally accept a closure as its argument. I'm writing this on my phone so forgive any syntax errors or accidental omissions: class

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-03 Thread Nate Birkholz via swift-users
Thanks, the second had occurred to me, but felt a little too much like in practice it would make the code harder to understand. On Fri, Jun 2, 2017 at 9:58 PM, Zhao Xin wrote: > I found two workarounds. > > 1. > > protocol Foo: class { > > func bar() > > } > > > class

Re: [swift-users] Calling default implementation of protocol methods as selectors

2017-06-02 Thread Zhao Xin via swift-users
I found two workarounds. 1. protocol Foo: class { func bar() } class Base:Foo { @objc func bar() { print("bar") } } class Baz: Base { override init() { super.init() let tapRecognizer = UITapGestureRecognizer(target: self, action: