我正在尝试使用 Swinject 注入依赖项,但我不知道我做错了什么。
我有处理注册用户的协议。
protocol AuthServiceProtocol {
func registerUser(email: String, password: String, completion: @escaping CompletionHandler) }
和一个符合这个协议的类使所有的逻辑:
class AuthService: AuthServiceProtocol {
func registerUser(email: String, password: String, completion: @escaping CompletionHandler) {
let lowerCaseMail = email.lowercased()
let body: [String: Any] = [
"email": lowerCaseMail,
"password" : password
]
Alamofire.request(URL_REGISTER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: HEADER).responseString { (response) in
if response.result.error == nil {
completion(true)
} else {
completion(false)
debugPrint(response.result.error as Any)
}
}
}
}
因此,在 AppDelegate 中我们注册了容器,它看起来像:
let container = Container() { container in
container.register(AuthServiceProtocol.self) { _ in AuthService() }.inObjectScope(.container)
container.register(CreateAccountVC.self) { r in
let controller = CreateAccountVC()
controller.authService = r.resolve(AuthServiceProtocol.self)
return controller
}
}
但在 CreateAccountVC authService 中为空。有什么想法我该怎么做?CreateAccountVC 是 ViewController 的子类,我已经通过属性和构造函数尝试过它,但它一直为零。