@selector() in Swift? -
i'm trying create nstimer in swift i'm having trouble.
nstimer(timeinterval: 1, target: self, selector: test(), userinfo: nil, repeats: true) test() function in same class.
i error in editor:
could not find overload 'init' accepts supplied arguments
when change selector: test() selector: nil error disappears.
i've tried:
selector: test()selector: testselector: selector(test())
but nothing works , can't find solution in references.
swift itself doesn't use selectors — several design patterns in objective-c make use of selectors work differently in swift. (for example, use optional chaining on protocol types or is/as tests instead of respondstoselector:, , use closures wherever can instead of performselector: better type/memory safety.)
but there still number of important objc-based apis use selectors, including timers , target/action pattern. swift provides selector type working these. (swift automatically uses in place of objc's sel type.)
in swift 2.2 (xcode 7.3) , later (including swift 3 / xcode 8):
you can construct selector swift function type using #selector expression.
let timer = timer(timeinterval: 1, target: object, selector: #selector(myclass.test), userinfo: nil, repeats: false) button.addtarget(object, action: #selector(myclass.buttontapped), for: .touchupinside) view.perform(#selector(uiview.insertsubview(_:abovesubview:)), with: button, with: otherbutton) the great thing approach? function reference checked swift compiler, can use #selector expression class/method pairs exist , eligible use selectors (see "selector availability" below). you're free make function reference specific need, per the swift 2.2+ rules function-type naming.
(this improvement on objc's @selector() directive, because compiler's -wundeclared-selector check verifies named selector exists. swift function reference pass #selector checks existence, membership in class, , type signature.)
there couple of caveats function references pass #selector expression:
- multiple functions same base name can differentiated parameter labels using aforementioned syntax function references (e.g.
insertsubview(_:at:)vsinsertsubview(_:abovesubview:)). if function has no parameters, way disambiguate useascast function's type signature (e.g.foo () -> ()vsfoo(_:)). - there's special syntax property getter/setter pairs in swift 3.0+. example, given
var foo: int, can use#selector(getter: myclass.foo)or#selector(setter: myclass.foo).
general notes:
cases #selector doesn't work, , naming: don't have function reference make selector (for example, methods dynamically registered in objc runtime). in case, can construct selector string: e.g. selector("dynamicmethod:") — though lose compiler's validity checking. when that, need follow objc naming rules, including colons (:) each parameter.
selector availability: method referenced selector must exposed objc runtime. case if it's in class (ultimately) inherits nsobject, if it's in pure swift class you'll need preface method's declaration @objc. remember private symbols aren't exposed runtime, — method needs have @ least internal visibility.
key paths: these related not quite same selectors. there's special syntax these in swift 3, too: e.g. chris.valueforkeypath(#keypath(person.friends.firstname)). see se-0062 details.
you can read more selectors under interacting objective-c apis in using swift cocoa , objective-c.
note: before swift 2.2, selector conformed stringliteralconvertible, might find old code bare strings passed apis take selectors. you'll want run "convert current swift syntax" in xcode using #selector.
Comments
Post a Comment