-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't pass arguments on "register" #45
Comments
I think, in your case, Swift compiler cannot infer the type of 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 notifications@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 Thank you for your help😃 |
I'm glad I could help! :) Em qua, 23 de mar de 2016 às 12:54, Yoichi Tagaya notifications@github.com
|
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).
The text was updated successfully, but these errors were encountered: