Page 1 of 1

ios 如何更合理的注入 protocol

Posted: 2024年 Jun 24日 20:56
by zerozheng

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这种方式。


Re: ios 如何更合理的注入 protocol

Posted: 2024年 Jul 12日 10:26
by OS App - 尼奥

registerService 方法目前只能注入“ UI 业务包中未实现的 Protocol ”,可以注入的 Protocol 一般是在对应 UI 业务包的文档中会有说明。

暂时还不支持针对“ UI 业务包中已实现的 Protocol ”替换单个 API 的诉求。