swift 2.2 #selector on var

swift 2.2 #selector on var

我使用 Typhoon,我需要注入选择器 属性

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

但编译器看不到选择器定义为 var.

例如: 如果我有 var viewModel: AuthViewModel!

使用#selector(viewModel)

所以我得到 Argument of '#selector' cannot refer to a property

更新:

我在 Swift 中使用过 Typhoon 时遇到问题。我解决了我的问题,例如放弃 Typhoon 并改用 Swinject。 现在我还可以使用 struct 进行注入等等。

您对可以传递给#selector()的内容有误解。您绝对应该阅读 Eric D 发布的 Whosebug 问题:

概述 #selector() 的用途:

无法将Swift 属性传递给#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.

- 引用自@selector() in Swift?

简而言之,您不能传入 var,您需要传递一个 Objective-C 运行时可以识别的函数。它要么是 Objective-C 方法,要么是标记为 @objc.

的 Swift 方法