1

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! and

use #selector(viewModel)

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

UPDATE:

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.

ajjnix
  • 462
  • 3
  • 17

1 Answers1

1

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.

Community
  • 1
  • 1
Infinity James
  • 4,667
  • 5
  • 23
  • 36
  • AuthViewModel subclass from NSObject and class used viewModel also subclass UIViewController. What can I do? If I use Selector("viewModel") it work but I have warning. But I can't use #selector because viewModel is "var"? – ajjnix Apr 05 '16 at 11:20