ios - Swift #selector in subclass -
after updated xcode, can build code ios 9.3 now, have lot of warnings updating syntax conform upcoming swift version 3.
many warnings selector, i'm using this:
class foo { func registerpan() { let recognizer = uipangesturerecognizer(target: self, action: selector("handlepan:")) //do recognizer } func handlepan(recognizer:uipangesturerecognizer) { //handle pan gesture } } class bar: foo { override func handlepan(recognizer:uipangesturerecognizer) { //overriding handle pan gesture } } so instances of foo , bar have own ways handle pan gesture. xcode suggests me change method use selectors, use #selector expression. automatic suggested change editor is:
#selector(foo.handlepan(_:)) here comes question. in previous version, specific implementation called base class , subclass. now, looks function of foo called instance of bar class (bar inherits registerpan function foo). correct?
probably xcode static analysis doesn't care inheritance here. i'm considering changing these selectors to:
#selector(self.handlepan(_:)) so instance of whatever class calls own implementation, want sure if it's idea.
as apple docs say:
a selector expression lets access selector used refer method in objective-c.
#selector(method name) the method name must reference method available in objective-c runtime. value of selector expression instance of selector type. example:
class someclass: nsobject { @objc(dosomethingwithint:) func dosomething(x: int) { } } let x = someclass() let selector = #selector(x.dosomething(_:)) ..because selector created @ compile time, not @ runtime, compiler can check method exists , method exposed objective-c runtime.
also can make extension of selector inside vc('s) , declare selectors there:
private extension selector { static let updateitems = #selector(characterslistvc.updateitems) static let getmoreitems = #selector(characterslistvc.getmoreitems) } and use way:
button?.addtarget(self, action: .updateitems, forcontrolevents: .touchupinside) hope helps :)
Comments
Post a Comment