For some reason I can't pass arguments to "register" method, getting error "Cannot invoke 'register' with an argument list of type '(BaseFetchDelegate.Type, name: String, (_, _) -> BaseFetchDelegate)'"
It works correctly if I remove the second argument (table).
container.register(BaseFetchDelegate.self, name: "tasksFetchDelegate") { (r,table) in
return BaseFetchDelegate()
}
I think, in your case, Swift compiler cannot infer the type of table because the parameter is not used in the closure. If you specify the type explicitly or use table in the closure to infer its type, you can pass the argument to register.
container.register(BaseFetchDelegate.self, name: "tasksFetchDelegate") {
(r: ResolverType, table: TableType) in // Specify the type explicitly like this
// Or use `table` anywhere in the closure to infer its type.
return BaseFetchDelegate()
}
it works, thanks )
Good to hear!
Hi,
I had the problem when trying to use arguments. After explicitly declare the type of the arguments, I was not able to successfully resolve a registered _ServiceFactory_.
It took me a while, but I noticed, after read the implementation, that the _ServiceKey_ uses the arguments types to map the _ServiceFactories_. I was using a protocol as a type of the argument, and when trying to resolve the dependency, I was using the implementation. To solve the problem, I had to do:
var obj = MyProtocolImplementation()
container.resolve(MyClass.self, argument: obj as MyProtocol)!
For me, it wasn't an obvious detail. I think it'd be great to add a note on documentation about those things.
Hi @brunopinheiro, thank you for posting the issue and sorry for the bad experience😥
Currently this page has a document about the service key, but it might not be so obvious. Would you give me suggestion to improve the document? Maybe adding a trouble shooting section is better?
Hi @yoichitgy,
My bad... Sorry. I didn't read or pay enough attention to this section on documentation.
Well... in that case, as a suggestion, I think you could add a note explaining when do you need to explicitly define the type of the arguments, and add an example like this one:
class PetOwner {
var pet: AnimalType
}
container.register(PetOwner.self) { (_: ResolverType, pet: AnimalType) in
var petOwner = PetOwner()
petOwner.pet = pet
return petOwner
}
var cat = Cat(name: "Bilbo")
// Won't work, since we don't have any Registration Key matching [PetOwner, (Cat) -> PetOwner]
var petOwner = container.resolve(PetOwner.self, argument: cat)!
// This is the correct Registration Key [PetOwner, (AnimalType) -> PertOwner]
var petOwner = container.resolve(PetOwner.self, argument: cat as AnimalType)!
Thanks @brunopinheiro for the suggestion😀 I'll update the documentation shortly as #70.
Nice! Thanks Yoichi!
Em ter, 22 de mar de 2016 Ã s 10:26, Yoichi Tagaya [email protected]
escreveu:
Thanks @brunopinheiro https://github.com/brunopinheiro for the
suggestion😀 I'll update the documentation shortly as #70
https://github.com/Swinject/Swinject/issues/70.—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/Swinject/Swinject/issues/45#issuecomment-199814249
@brunopinheiro I updated the document. I modified your suggestion slightly but it was very helpful because I didn't have to think from scratch. 5b2d0474e43a2737864db4ab84854a68c22bbe73
Thank you for your help😃
I'm glad I could help! :)
Thanks for your attention, Yoichi!
Em qua, 23 de mar de 2016 Ã s 12:54, Yoichi Tagaya [email protected]
escreveu:
@brunopinheiro https://github.com/brunopinheiro I updated the document.
I modified your suggestion slightly but it was very helpful because I
didn't have to think from scratch. 5b2d047
https://github.com/Swinject/Swinject/commit/5b2d0474e43a2737864db4ab84854a68c22bbe73—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/Swinject/Swinject/issues/45#issuecomment-200407631