Can I use a @protocol for interfacing between classes? My main goal is to do some dependency injection like in Java (with interfaces and implements).
I’ve got the following classes: SignUpServiceImpl (which has an interface called SignUpService) and ServiceHelperImpl (interface is ServiceHelper).
I don’t want to hard wire both implementations together so I use a @protocol in ServiceHelper which is implemented by ServiceHelperImpl. Then SignUpServiceImpl is initialized with ServiceHelper like this:
- (id)initWithHelper:(ServiceHelper *)myServiceHelper
Is what I’m trying to accomplish possible? It looks so much easier in Java….
An objc protocol is very similar to a Java interface.
The blocking point for you may be how you expect things are actually tied together — or protocol syntax.
Declare a protocol:
Use it in a class:
MONHelperadopts the protocol:In use: