我正在尝试使用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的子类,我已经尝试通过属性和构造函数对其进行尝试,但它一直都为零。

最佳答案

检查您的代码:

var container : Container {
        let container = Container()
        container.register(AuthServiceProtocol.self) { _ in AuthService() }.inObjectScope(.container)
        container.register(CreateAccountVC.self) { r in
            let controller = CreateAccountVC()
            controller.authService = r.resolve(AuthServiceProtocol.self)
            print(r.resolve(AuthServiceProtocol.self))
            return controller
        }

        return container
    }

您已经计算了属性,每次调用它都会创建一个NEW Container对象。

将代码重构为只有一个容器,我相信您会很好。

编辑:

这是一个工作代码段。
下面是一个小型包装器类,用于抽象具体的DI服务(以防Swinject有一天被其他东西取代):
import Swinject

public class ConfigurationProvider {

    // Currently using Swinject
    private let backingService = Container()

    // Singleton
    public static let shared = ConfigurationProvider()

    // Hidden initializer
    private init() {}


    // MARK: - Bind / Resolve

    public func bind<T>(interface: T.Type, to assembly: T) {
        backingService.register(interface) { _ in assembly }
    }

    public func resolve<T>(interface: T.Type) -> T! {
        return backingService.resolve(interface)
    }
}


// Extension methods to ignore 'shared.' call, like:
// ConfigurationProvider.bind(interface: IAssembly, to: Assembly())
// ConfigurationProvider.resolve(interface: IAssembly)

public extension ConfigurationProvider {

    static func bind<T>(interface: T.Type, to assembly: T) {
        ConfigurationProvider.shared.bind(interface: interface, to: assembly)
    }

    static func resolve<T>(interface: T.Type) -> T! {
        return ConfigurationProvider.shared.resolve(interface: interface)
    }
}

用法:
class RSAuthLoginModuleAssembly: IAuthLoginModuleAssembly {

}

// Register:

ConfigurationProvider.bind(interface: IAuthLoginModuleAssembly.self, to: ConcreteAuthLoginModuleAssembly())

// Resolve:

    guard let assembly = ConfigurationProvider.resolve(interface: IAuthLoginModuleAssembly.self) else {
        throw NSError(domain: "Assembly cannot be nil", code: 999, userInfo: nil)
    }

10-08 05:44