ios 如何更合理的注入 protocol
Posted: 2024年 Jun 24日 20:56
ThingSmartBizCore 目前提供了以下两个方法来注册服务:
Code: Select all
/**
* Register a instance for service which can not be served by BizBundle itself
* Each service can only register one instance or class at a time, whichever is the last
*
* @param service service protocol
* @param instance instance which conform to the service protocol, strong reference
* unregister if nil
*/
- (void)registerService:(Protocol *)service withInstance:(id)instance;
/**
* Register a class for service which can not be served by BizBundle itself
* Each service can only register one instance or class at a time, whichever is the last
*
*
* @param service service protocol
* @param cls class which conform to the service protocol, [cls new] to get instance
* unregister if nil
*/
- (void)registerService:(Protocol *)service withClass:(Class)cls;
其中以instance的方法注册的服务不能通过 respondsToSelector 判断是否支持某个方法,通过class注册的服务可以。因此推荐使用class这种方式。