ajjnix ajjnix - 10 months ago 41
Swift Question

swift 2.2 #selector on var

I use Typhoon and I need selector for injection property

definition.injectProperty(Selector("viewModel"), with: self.viewModel.inviteViewModel())

but compiler don't see selector define as var.

for example:
if I have
var viewModel: AuthViewModel!


so I get
Argument of '#selector' cannot refer to a property


I have the problem from used Typhoon in Swift. I resolve my problem like renouncement from Typhoon and switch to Swinject.
Also now I can use struct for injection and much more.


You have a misunderstanding of what can be passed into #selector(). You should definitely read the StackOverflow questions that Eric D posted:

Overview on what you can use #selector() for: Understanding Swift 2.2 Selector Syntax - #selector()

The reason why you can not pass a Swift property in to #selector():

Selector availability: The method referenced by the selector must be exposed to the ObjC runtime. This is already the case if it's in a class that (ultimately) inherits from NSObject, but if it's in a pure Swift class you'll need to preface that method's declaration with @objc. Remember that private symbols aren't exposed to the runtime, too — your method needs to have at least internal visibility.

- Referenced from @selector() in Swift?

In short, you can't pass in a var and you need to pass a function that the Objective-C runtime is aware of. Either it is an Objective-C method, or it is a Swift method marked as @objc.