Swinject: Ambiguous reference to member 'register(_:name:factory:) Error

Created on 9 Jun 2016  路  10Comments  路  Source: Swinject/Swinject

When I try to load my ViewController, I want to inject my ViewModel that have some arguments.
Something like:

container.register(ViewModel.self) { r, string1, string2 in
    ViewModel(string1: string1, string2: string2, api: r.resolve(APIProtocol.self)!)
}
container.register(ViewController.self) { r, string1, string2 in
    ViewController(viewModel: r.resolve(ViewModel.self, arguments: (string1, string2))!)
}

But I got this error

 Ambiguous reference to member 'register(_:name:factory:)
doc question

Most helpful comment

Compiler is unable to infer types of string1 and string2 parameters in

container.register(ViewController.self) { r, string1, string2 in
    ViewController(viewModel: r.resolve(ViewModel.self, arguments: (string1, string2))!)
}

Changing it to

container.register(ViewController.self) { (r, string1: String, string2: String) in
    ViewController(viewModel: r.resolve(ViewModel.self, arguments: (string1, string2))!)
}

should help.

All 10 comments

Compiler is unable to infer types of string1 and string2 parameters in

container.register(ViewController.self) { r, string1, string2 in
    ViewController(viewModel: r.resolve(ViewModel.self, arguments: (string1, string2))!)
}

Changing it to

container.register(ViewController.self) { (r, string1: String, string2: String) in
    ViewController(viewModel: r.resolve(ViewModel.self, arguments: (string1, string2))!)
}

should help.

Thanks @jakubvano for the exact answer!

thanks @jakubvano.

please, help me guys? What is wrong here:

defaultContainer.register(APIModel.self, name:"FileAPI") { r in
return APIModel(routeName: "files", api: r.resolve(AuthenticationAPI.self), defaultAPIVersion: "v3")
}

i'm getting the same error message you had.

I am using 1.1.2 version, try it, it may solve your problem for now. @igordeoliveirasa

@igordeoliveirasa I've noticed you are not unwrapping optional r.resolve(AuthenticationAPI.self) - is your constructor accepting AuthenticationAPI?? If not than changing it to r.resolve(AuthenticationAPI.self)! might solve your issue.

@jsaguiar We should update the docs in https://github.com/Swinject/Swinject/blob/master/Documentation/DIContainer.md#registration-with-arguments-to-di-container

Reopen until added?

@ened There is a remark section explicitly warning about using the exact argument types. Also, in current version there is a log output clarifying reason for resolution failure, which should help debugging these types of issues.

I can see that e.g. String, String? and String! being different types may not be clear - especially to Swift newcomers. Feel free to make PR extending the remark section with this distinction 馃槈

Updated the doc as commit 65b7851590b168108b1f4d17dc0bec5c16e5e5ee.

Was this page helpful?
0 / 5 - 0 ratings