NSObject(FWAppearance)
@interface NSObject (FWAppearance)
/// 从 appearance 里取值并赋值给当前实例,通常在对象的 init 里调用
- (void)fw_applyAppearance NS_REFINED_FOR_SWIFT;
@end
Undocumented
-
从 appearance 里取值并赋值给当前实例,通常在对象的 init 里调用
Declaration
Objective-C
- (void)fw_applyAppearance;
-
是否是非Null(nil, NSNull)
Declaration
Objective-C
@property (nonatomic, readonly) BOOL fw_isNotNull;
Return Value
如果为非Null返回YES,为Null返回NO
-
是否是非空对象(nil, NSNull, count为0, length为0)
Declaration
Objective-C
@property (nonatomic, readonly) BOOL fw_isNotEmpty;
Return Value
如果是非空对象返回YES,为空对象返回NO
-
检测并安全转换为NSInteger
Declaration
Objective-C
@property (nonatomic, readonly) NSInteger fw_safeInteger;
Return Value
NSInteger
-
检测并安全转换为Float
Declaration
Objective-C
@property (nonatomic, readonly) float fw_safeFloat;
Return Value
Float
-
检测并安全转换为Double
Declaration
Objective-C
@property (nonatomic, readonly) double fw_safeDouble;
Return Value
Double
-
检测并安全转换为Bool
Declaration
Objective-C
@property (nonatomic, readonly) BOOL fw_safeBool;
Return Value
Bool
-
检测并安全转换为NSNumber
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSNumber *fw_safeNumber;
Return Value
NSNumber
-
检测并安全转换为NSString
Declaration
Objective-C
@property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_safeString;
Return Value
NSString
-
检测并安全转换为NSDate
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSDate *fw_safeDate;
Return Value
NSDate
-
检测并安全转换为NSData
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSData *fw_safeData;
Return Value
NSData
-
检测并安全转换为NSArray
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSArray *fw_safeArray;
Return Value
NSArray
-
检测并安全转换为NSMutableArray
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSMutableArray *fw_safeMutableArray;
Return Value
NSMutableArray
-
检测并安全转换为NSDictionary
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSDictionary *fw_safeDictionary;
Return Value
NSDictionary
-
检测并安全转换为NSMutableDictionary
Declaration
Objective-C
@property (nonatomic, strong, readonly) NS_REFINED_FOR_SWIFT NSMutableDictionary *fw_safeMutableDictionary;
Return Value
NSMutableDictionary
-
执行加锁(支持任意对象),等待信号量,自动创建信号量
Declaration
Objective-C
- (void)fw_lock;
-
执行解锁(支持任意对象),发送信号量,自动创建信号量
Declaration
Objective-C
- (void)fw_unlock;
-
延迟delay秒后主线程执行,返回可取消的block,对象范围
Declaration
Objective-C
- (nonnull id)fw_performBlock:(nonnull void (^)(id _Nonnull __strong))block afterDelay:(NSTimeInterval)delay;
-
延迟delay秒后后台线程执行,返回可取消的block,对象范围
Declaration
Objective-C
- (nonnull id)fw_performBlockInBackground: (nonnull void (^)(id _Nonnull __strong))block afterDelay:(NSTimeInterval)delay;
-
延迟delay秒后指定线程执行,返回可取消的block,对象范围
Declaration
Objective-C
- (nonnull id)fw_performBlock:(nonnull void (^)(id _Nonnull __strong))block onQueue:(nonnull dispatch_queue_t)queue afterDelay:(NSTimeInterval)delay;
-
同一个identifier仅执行一次block,对象范围
Declaration
Objective-C
- (void)fw_performOnce:(nonnull NSString *)identifier withBlock:(nonnull void (^)(void))block;
-
延迟delay秒后主线程执行,返回可取消的block,全局范围
Declaration
Objective-C
+ (nonnull id)fw_performBlock:(nonnull void (^)(void))block afterDelay:(NSTimeInterval)delay;
-
延迟delay秒后后台线程执行,返回可取消的block,全局范围
Declaration
Objective-C
+ (nonnull id)fw_performBlockInBackground:(nonnull void (^)(void))block afterDelay:(NSTimeInterval)delay;
-
延迟delay秒后指定线程执行,返回可取消的block,全局范围
Declaration
Objective-C
+ (nonnull id)fw_performBlock:(nonnull void (^)(void))block onQueue:(nonnull dispatch_queue_t)queue afterDelay:(NSTimeInterval)delay;
-
取消指定延迟block,全局范围
Declaration
Objective-C
+ (void)fw_cancelBlock:(nonnull id)block;
-
同步方式执行异步block,阻塞当前线程(信号量),异步block必须调用completionHandler,全局范围
Declaration
Objective-C
+ (void)fw_syncPerformAsyncBlock: (nonnull void (^)(void (^_Nonnull __strong)(void)))asyncBlock;
-
同一个identifier仅执行一次block,全局范围
Declaration
Objective-C
+ (void)fw_performOnce:(nonnull NSString *)identifier withBlock:(nonnull void (^)(void))block;
-
重试方式执行异步block,直至成功或者次数为0(小于0不限)或者超时(小于等于0不限),完成后回调completion。block必须调用completionHandler,参数示例:重试4次|超时8秒|延迟2秒
Declaration
Objective-C
+ (void)fw_performBlock:(nonnull void (^)(void (^_Nonnull __strong)( BOOL, id _Nullable __strong)))block completion:(nonnull void (^)(BOOL, id _Nullable __strong))completion retryCount:(NSInteger)retryCount timeoutInterval:(NSTimeInterval)timeoutInterval delayInterval:(nonnull NSTimeInterval (^)(NSInteger))delayInterval isCancelled:(nullable BOOL (^)(void))isCancelled;
-
执行轮询block任务,返回任务Id可取消
Declaration
Objective-C
+ (nonnull NSString *)fw_performTask:(nonnull void (^)(void))task start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats async:(BOOL)async;
-
指定任务Id取消轮询任务
Declaration
Objective-C
+ (void)fw_cancelTask:(nonnull NSString *)taskId;
-
监听某个点对点消息,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *) fw_observeMessage:(nonnull NSNotificationName)name block: (nonnull void (^)(NSNotification *_Nonnull __strong))block;
Parameters
name
消息名称
block
消息句柄
Return Value
监听唯一标志
-
监听某个指定对象点对点消息,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *) fw_observeMessage:(nonnull NSNotificationName)name object:(nullable id)object block: (nonnull void (^)(NSNotification *_Nonnull __strong))block;
Parameters
name
消息名称
object
消息对象,值为nil时表示所有
block
消息句柄
Return Value
监听唯一标志
-
监听某个点对点消息,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *)fw_observeMessage:(nonnull NSNotificationName)name target:(nullable id)target action:(nonnull SEL)action;
Parameters
name
消息名称
target
消息目标
action
目标动作,参数为通知对象
Return Value
监听唯一标志
-
监听某个指定对象点对点消息,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *)fw_observeMessage:(nonnull NSNotificationName)name object:(nullable id)object target:(nullable id)target action:(nonnull SEL)action;
Parameters
name
消息名称
object
消息对象,值为nil时表示所有
target
消息目标
action
目标动作,参数为通知对象
Return Value
监听唯一标志
-
手工移除某个点对点消息指定监听
Declaration
Objective-C
- (void)fw_unobserveMessage:(nonnull NSNotificationName)name target:(nullable id)target action:(nullable SEL)action;
Parameters
name
消息名称
target
消息目标
action
目标动作
-
手工移除某个指定对象点对点消息指定监听
Declaration
Objective-C
- (void)fw_unobserveMessage:(nonnull NSNotificationName)name object:(nullable id)object target:(nullable id)target action:(nullable SEL)action;
Parameters
name
消息名称
object
消息对象,值为nil时表示所有
target
消息目标
action
目标动作
-
手工移除某个指定对象点对点消息指定监听
Declaration
Objective-C
- (void)fw_unobserveMessage:(nonnull NSNotificationName)name identifier:(nonnull NSString *)identifier;
Parameters
name
消息名称
identifier
监听唯一标志
-
手工移除某个点对点消息所有监听
Declaration
Objective-C
- (void)fw_unobserveMessage:(nonnull NSNotificationName)name;
Parameters
name
消息名称
-
手工移除某个指定对象点对点消息所有监听
Declaration
Objective-C
- (void)fw_unobserveMessage:(nonnull NSNotificationName)name object:(nullable id)object;
Parameters
name
消息名称
object
消息对象,值为nil时表示所有
-
手工移除所有点对点消息监听
Declaration
Objective-C
- (void)fw_unobserveAllMessages;
-
发送点对点消息
Declaration
Objective-C
- (void)fw_sendMessage:(nonnull NSNotificationName)name toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
receiver
消息接收者
-
发送点对点消息,附带对象
Declaration
Objective-C
- (void)fw_sendMessage:(nonnull NSNotificationName)name object:(nullable id)object toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
object
消息对象
receiver
消息接收者
-
发送点对点消息,附带对象和用户信息
Declaration
Objective-C
- (void)fw_sendMessage:(nonnull NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
object
消息对象
userInfo
用户信息
receiver
消息接收者
-
发送类点对点消息
Declaration
Objective-C
+ (void)fw_sendMessage:(nonnull NSNotificationName)name toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
receiver
消息接收者
-
发送类点对点消息,附带对象
Declaration
Objective-C
+ (void)fw_sendMessage:(nonnull NSNotificationName)name object:(nullable id)object toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
object
消息对象
receiver
消息接收者
-
发送类点对点消息,附带对象和用户信息
Declaration
Objective-C
+ (void)fw_sendMessage:(nonnull NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo toReceiver:(nonnull id)receiver;
Parameters
name
消息名称
object
消息对象
userInfo
用户信息
receiver
消息接收者
-
监听某个广播通知,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *) fw_observeNotification:(nonnull NSNotificationName)name block:(nonnull void (^)(NSNotification *_Nonnull __strong)) block;
Parameters
name
通知名称
block
通知句柄
Return Value
监听唯一标志
-
监听某个指定对象广播通知,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *) fw_observeNotification:(nonnull NSNotificationName)name object:(nullable id)object block:(nonnull void (^)(NSNotification *_Nonnull __strong)) block;
Parameters
name
通知名称
object
通知对象,值为nil时表示所有
block
通知句柄
Return Value
监听唯一标志
-
监听某个广播通知,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *)fw_observeNotification:(nonnull NSNotificationName)name target:(nullable id)target action:(nonnull SEL)action;
Parameters
name
通知名称
target
通知目标
action
目标动作,参数为通知对象
Return Value
监听唯一标志
-
监听某个指定对象广播通知,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *)fw_observeNotification:(nonnull NSNotificationName)name object:(nullable id)object target:(nullable id)target action:(nonnull SEL)action;
Parameters
name
通知名称
object
通知对象,值为nil时表示所有
target
通知目标
action
目标动作,参数为通知对象
Return Value
监听唯一标志
-
手工移除某个广播通知指定监听
Declaration
Objective-C
- (void)fw_unobserveNotification:(nonnull NSNotificationName)name target:(nullable id)target action:(nullable SEL)action;
Parameters
name
通知名称
target
通知目标
action
目标动作
-
手工移除某个指定对象广播通知指定监听
Declaration
Objective-C
- (void)fw_unobserveNotification:(nonnull NSNotificationName)name object:(nullable id)object target:(nullable id)target action:(nullable SEL)action;
Parameters
name
通知名称
object
通知对象,值为nil时表示所有
target
通知目标
action
目标动作
-
手工移除某个指定对象广播通知指定监听
Declaration
Objective-C
- (void)fw_unobserveNotification:(nonnull NSNotificationName)name identifier:(nonnull NSString *)identifier;
Parameters
name
通知名称
identifier
监听唯一标志
-
手工移除某个广播通知所有监听
Declaration
Objective-C
- (void)fw_unobserveNotification:(nonnull NSNotificationName)name;
Parameters
name
通知名称
-
手工移除某个指定对象广播通知所有监听
Declaration
Objective-C
- (void)fw_unobserveNotification:(nonnull NSNotificationName)name object:(nullable id)object;
Parameters
name
通知名称
object
通知对象,值为nil时表示所有
-
手工移除所有广播通知监听
Declaration
Objective-C
- (void)fw_unobserveAllNotifications;
-
发送广播通知
Declaration
Objective-C
- (void)fw_postNotification:(nonnull NSNotificationName)name;
Parameters
name
通知名称
-
发送广播通知,附带对象
Declaration
Objective-C
- (void)fw_postNotification:(nonnull NSNotificationName)name object:(nullable id)object;
Parameters
name
通知名称
object
通知对象
-
发送广播通知,附带对象和用户信息
Declaration
Objective-C
- (void)fw_postNotification:(nonnull NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo;
Parameters
name
通知名称
object
通知对象
userInfo
用户信息
-
发送广播通知
Declaration
Objective-C
+ (void)fw_postNotification:(nonnull NSNotificationName)name;
Parameters
name
通知名称
-
发送广播通知,附带对象
Declaration
Objective-C
+ (void)fw_postNotification:(nonnull NSNotificationName)name object:(nullable id)object;
Parameters
name
通知名称
object
通知对象
-
发送广播通知,附带对象和用户信息
Declaration
Objective-C
+ (void)fw_postNotification:(nonnull NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo;
Parameters
name
通知名称
object
通知对象
userInfo
用户信息
-
监听对象某个属性,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *) fw_observeProperty:(nonnull NSString *)property block:(nonnull void (^)(id _Nonnull __strong, NSDictionary<NSKeyValueChangeKey, id> *_Nonnull __strong))block;
Parameters
property
属性名称
block
目标句柄,block参数依次为object、优化的change字典(不含NSNull)
Return Value
监听唯一标志
-
监听对象某个属性,对象释放时自动移除监听,添加多次执行多次
Declaration
Objective-C
- (nonnull NSString *)fw_observeProperty:(nonnull NSString *)property target:(nullable id)target action:(nonnull SEL)action;
Parameters
property
属性名称
target
目标对象
action
目标动作,action参数依次为object、优化的change字典(不含NSNull)
Return Value
监听唯一标志
-
手工移除某个属性指定监听
Declaration
Objective-C
- (void)fw_unobserveProperty:(nonnull NSString *)property target:(nullable id)target action:(nullable SEL)action;
Parameters
property
属性名称
target
目标对象,值为nil时移除所有对象(同UIControl)
action
目标动作,值为nil时移除所有动作(同UIControl)
-
手工移除某个属性指定监听
Declaration
Objective-C
- (void)fw_unobserveProperty:(nonnull NSString *)property identifier:(nonnull NSString *)identifier;
Parameters
property
属性名称
identifier
监听唯一标志
-
手工移除某个属性所有监听
Declaration
Objective-C
- (void)fw_unobserveProperty:(nonnull NSString *)property;
Parameters
property
属性名称
-
手工移除所有属性所有监听
Declaration
Objective-C
- (void)fw_unobserveAllProperties;
-
从json创建对象,线程安全。NSDate会按照UTC时间解析,下同
Declaration
Objective-C
+ (nullable instancetype)fw_modelWithJson:(nonnull id)json;
Parameters
json
json对象,支持NSDictionary、NSString、NSData
Return Value
实例对象,失败为nil
-
从字典创建对象,线程安全
Declaration
Objective-C
+ (nullable instancetype)fw_modelWithDictionary: (nonnull NSDictionary *)dictionary;
Parameters
dictionary
字典数据
Return Value
实例对象,失败为nil
-
从json创建Model数组
Declaration
Objective-C
+ (nullable NSArray *)fw_modelArrayWithJson:(nonnull id)json;
Parameters
json
json对象,支持NSDictionary、NSString、NSData
Return Value
Model数组
-
从json创建Model字典
Declaration
Objective-C
+ (nullable NSDictionary *)fw_modelDictionaryWithJson:(nonnull id)json;
Parameters
json
json对象,支持NSDictionary、NSString、NSData
Return Value
Model字典
-
从json对象设置对象属性
Declaration
Objective-C
- (BOOL)fw_modelSetWithJson:(nonnull id)json;
Parameters
json
json对象,支持NSDictionary、NSString、NSData
Return Value
是否设置成功
-
从字典设置对象属性
Declaration
Objective-C
- (BOOL)fw_modelSetWithDictionary:(nonnull NSDictionary *)dictionary;
Parameters
dictionary
字典数据
Return Value
是否设置成功
-
转换为json对象
Declaration
Objective-C
- (nullable id)fw_modelToJsonObject;
Return Value
json对象,如NSDictionary、NSArray,失败为nil
-
转换为json字符串数据
Declaration
Objective-C
- (nullable NSData *)fw_modelToJsonData;
Return Value
NSData,失败为nil
-
转换为json字符串
Declaration
Objective-C
- (nullable NSString *)fw_modelToJsonString;
Return Value
NSString,失败为nil
-
从属性拷贝当前对象
Declaration
Objective-C
- (nullable id)fw_modelCopy;
Return Value
拷贝对象,失败为nil
-
对象编码
Declaration
Objective-C
- (void)fw_modelEncodeWithCoder:(nonnull NSCoder *)aCoder;
-
对象解码
Declaration
Objective-C
- (nonnull id)fw_modelInitWithCoder:(nonnull NSCoder *)aDecoder;
-
对象的hash编码
Declaration
Objective-C
- (NSUInteger)fw_modelHash;
-
比较Model
Declaration
Objective-C
- (BOOL)fw_modelIsEqual:(nonnull id)model;
-
对象描述
Declaration
Objective-C
- (nonnull NSString *)fw_modelDescription;
-
获取类方法列表,自动缓存,支持meta类(objc_getMetaClass)
Declaration
Objective-C
+ (nonnull NSArray<NSString *> *)fw_classMethods:(nonnull Class)clazz superclass:(BOOL)superclass;
Parameters
clazz
指定类
superclass
是否包含父类,包含则递归到NSObject
Return Value
方法列表
-
获取类属性列表,自动缓存,支持meta类(objc_getMetaClass)
Declaration
Objective-C
+ (nonnull NSArray<NSString *> *)fw_classProperties:(nonnull Class)clazz superclass:(BOOL)superclass;
Parameters
clazz
指定类
superclass
是否包含父类,包含则递归到NSObject
Return Value
属性列表
-
获取类Ivar列表,自动缓存,支持meta类(objc_getMetaClass)
Declaration
Objective-C
+ (nonnull NSArray<NSString *> *)fw_classIvars:(nonnull Class)clazz superclass:(BOOL)superclass;
Parameters
clazz
指定类
superclass
是否包含父类,包含则递归到NSObject
Return Value
Ivar列表
-
安全调用方法,如果不能响应,则忽略之
Declaration
Objective-C
- (nullable id)fw_invokeMethod:(nonnull SEL)aSelector;
Parameters
aSelector
要执行的方法
Return Value
id 方法执行后返回的值。如果无返回值,则为nil
-
安全调用方法,如果不能响应,则忽略之
Declaration
Objective-C
- (nullable id)fw_invokeMethod:(nonnull SEL)aSelector withObject:(nullable id)object;
Parameters
aSelector
要执行的方法
object
传递的方法参数,非id类型可使用桥接,如int a = 1;(__bridge id)(void *)a
Return Value
id 方法执行后返回的值。如果无返回值,则为nil
-
安全调用方法,支持多个参数
Declaration
Objective-C
- (nullable id)fw_invokeMethod:(nonnull SEL)aSelector withObjects:(nonnull NSArray *)objects;
Parameters
aSelector
要执行的方法
objects
传递的参数数组
Return Value
id 方法执行后返回的值。如果无返回值,则为nil
-
对super发送消息
Declaration
Objective-C
- (nullable id)fw_invokeSuperMethod:(nonnull SEL)aSelector;
Parameters
aSelector
要执行的方法,需返回id类型
Return Value
id 方法执行后返回的值
-
对super发送消息,可传递参数
Declaration
Objective-C
- (nullable id)fw_invokeSuperMethod:(nonnull SEL)aSelector withObject:(nullable id)object;
Parameters
aSelector
要执行的方法,需返回id类型
object
传递的方法参数
Return Value
id 方法执行后返回的值
-
安全调用内部属性获取方法,如果属性不存在,则忽略之
Note
如果iOS13系统UIView调用部分valueForKey:方法闪退,且没有好的替代方案,可尝试调用此方法
Declaration
Objective-C
- (nullable id)fw_invokeGetter:(nonnull NSString *)name;
Parameters
name
内部属性名称
Return Value
属性值
-
安全调用内部属性设置方法,如果属性不存在,则忽略之
Note
如果iOS13系统UIView调用部分valueForKey:方法闪退,且没有好的替代方案,可尝试调用此方法
Declaration
Objective-C
- (nullable id)fw_invokeSetter:(nonnull NSString *)name withObject:(nullable id)object;
Parameters
name
内部属性名称
object
传递的方法参数
Return Value
方法执行后返回的值
-
临时对象,强引用,支持KVO
Note
备注:key的几种形式的声明和使用,下同- 声明:static char kAssociatedObjectKey; 使用:&kAssociatedObjectKey
- 声明:static void *kAssociatedObjectKey = &kAssociatedObjectKey; 使用:kAssociatedObjectKey
- 声明和使用直接用getter方法的selector,如@selector(xxx)、_cmd
- 声明和使用直接用c字符串,如"kAssociatedObjectKey"
Declaration
Objective-C
@property (nonatomic, strong, nullable) id fw_tempObject;
-
读取关联属性
Declaration
Objective-C
- (nullable id)fw_propertyForName:(nonnull NSString *)name;
Parameters
name
属性名称
Return Value
属性值
-
设置强关联属性,支持KVO
Declaration
Objective-C
- (void)fw_setProperty:(nullable id)object forName:(nonnull NSString *)name;
Parameters
object
属性值
name
属性名称
-
设置赋值关联属性,支持KVO,注意可能会产生野指针
Declaration
Objective-C
- (void)fw_setPropertyAssign:(nullable id)object forName:(nonnull NSString *)name;
Parameters
object
属性值
name
属性名称
-
设置拷贝关联属性,支持KVO
Declaration
Objective-C
- (void)fw_setPropertyCopy:(nullable id)object forName:(nonnull NSString *)name;
Parameters
object
属性值
name
属性名称
-
设置弱引用关联属性,支持KVO,OC不支持weak关联属性
Declaration
Objective-C
- (void)fw_setPropertyWeak:(nullable id)object forName:(nonnull NSString *)name;
Parameters
object
属性值
name
属性名称
-
给对象绑定上另一个对象以供后续取出使用,如果 object 传入 nil 则会清除该 key 之前绑定的对象
Declaration
Objective-C
- (void)fw_bindObject:(nullable id)object forKey:(nonnull NSString *)key;
Parameters
object
对象,会被 strong 强引用
key
键名
-
给对象绑定上另一个弱引用对象以供后续取出使用,如果 object 传入 nil 则会清除该 key 之前绑定的对象
Declaration
Objective-C
- (void)fw_bindObjectWeak:(nullable id)object forKey:(nonnull NSString *)key;
Parameters
object
对象,不会被 strong 强引用
key
键名
-
取出之前使用 bind 方法绑定的对象
Declaration
Objective-C
- (nullable id)fw_boundObjectForKey:(nonnull NSString *)key;
Parameters
key
键名
-
给对象绑定上一个 double 值以供后续取出使用
Declaration
Objective-C
- (void)fw_bindDouble:(double)doubleValue forKey:(nonnull NSString *)key;
Parameters
doubleValue
double值
key
键名
-
取出之前用 bindDouble:forKey: 绑定的值
Declaration
Objective-C
- (double)fw_boundDoubleForKey:(nonnull NSString *)key;
Parameters
key
键名
-
给对象绑定上一个 BOOL 值以供后续取出使用
Declaration
Objective-C
- (void)fw_bindBool:(BOOL)boolValue forKey:(nonnull NSString *)key;
Parameters
boolValue
布尔值
key
键名
-
取出之前用 bindBool:forKey: 绑定的值
Declaration
Objective-C
- (BOOL)fw_boundBoolForKey:(nonnull NSString *)key;
Parameters
key
键名
-
给对象绑定上一个 NSInteger 值以供后续取出使用
Declaration
Objective-C
- (void)fw_bindInt:(NSInteger)integerValue forKey:(nonnull NSString *)key;
Parameters
integerValue
整数值
key
键名
-
取出之前用 bindInt:forKey: 绑定的值
Declaration
Objective-C
- (NSInteger)fw_boundIntForKey:(nonnull NSString *)key;
Parameters
key
键名
-
移除之前使用 bind 方法绑定的对象
Declaration
Objective-C
- (void)fw_removeBindingForKey:(nonnull NSString *)key;
Parameters
key
键名
-
移除之前使用 bind 方法绑定的所有对象
Declaration
Objective-C
- (void)fw_removeAllBindings;
-
返回当前有绑定对象存在的所有的 key 的数组,数组中元素的顺序是随机的,如果不存在任何 key,则返回一个空数组
Declaration
Objective-C
- (nonnull NSArray<NSString *> *)fw_allBindingKeys;
-
返回是否设置了某个 key
Declaration
Objective-C
- (BOOL)fw_hasBindingKey:(nonnull NSString *)key;
Parameters
key
键名
-
交换类实例方法。复杂情况可能会冲突
Declaration
Objective-C
+ (BOOL)fw_exchangeInstanceMethod:(nonnull SEL)originalSelector swizzleMethod:(nonnull SEL)swizzleSelector;
Parameters
originalSelector
原始方法
swizzleSelector
交换方法
Return Value
是否成功
-
交换类静态方法。复杂情况可能会冲突
Declaration
Objective-C
+ (BOOL)fw_exchangeClassMethod:(nonnull SEL)originalSelector swizzleMethod:(nonnull SEL)swizzleSelector;
Parameters
originalSelector
原始方法
swizzleSelector
交换方法
Return Value
是否成功
-
交换类实例方法为block实现。复杂情况可能会冲突
Note
swizzleBlock示例:^(__unsafe_unretained UIViewController selfObject, BOOL animated){ ((void()(id, SEL, BOOL))objc_msgSend)(selfObject, swizzleSelector, animated); }
Declaration
Objective-C
+ (BOOL)fw_exchangeInstanceMethod:(nonnull SEL)originalSelector swizzleMethod:(nonnull SEL)swizzleSelector withBlock:(nonnull id)swizzleBlock;
Parameters
originalSelector
原始方法
swizzleSelector
交换方法
swizzleBlock
实现block
Return Value
是否成功
-
交换类静态方法为block实现。复杂情况可能会冲突
Note
swizzleBlock示例:^(__unsafe_unretained Class selfClass, BOOL animated){ ((void(*)(id, SEL, BOOL))objc_msgSend)(selfClass, swizzleSelector, animated); }
Declaration
Objective-C
+ (BOOL)fw_exchangeClassMethod:(nonnull SEL)originalSelector swizzleMethod:(nonnull SEL)swizzleSelector withBlock:(nonnull id)swizzleBlock;
Parameters
originalSelector
原始方法
swizzleSelector
交换方法
swizzleBlock
实现block
Return Value
是否成功
-
生成原始方法对应的随机交换方法
Declaration
Objective-C
+ (nonnull SEL)fw_exchangeSwizzleSelector:(nonnull SEL)selector;
Parameters
selector
原始方法
Return Value
交换方法
-
通用swizzle替换方法为block实现,支持类和对象,identifier有值且相同时仅执行一次。复杂情况不会冲突,推荐使用
Declaration
Objective-C
+ (BOOL)fw_swizzleMethod:(nullable id)target selector:(nonnull SEL)originalSelector identifier:(nullable NSString *)identifier withBlock:(nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
target
目标类或对象
originalSelector
原始方法
identifier
唯一标识,有值且相同时仅执行一次
block
实现句柄
Return Value
是否成功
-
使用swizzle替换类实例方法为block实现。复杂情况不会冲突,推荐使用
Note
Swift实现代码示例: NSObject.fw.swizzleInstanceMethod(UIViewController.self, selector: NSSelectorFromString(“viewDidLoad”)) { targetClass, originalCMD, originalIMP in let swizzleIMP: @convention(block)(UIViewController) -> Void = { selfObject in typealias originalMSGType = @convention©(UIViewController, Selector) -> Void let originalMSG: originalMSGType = unsafeBitCast(originalIMP(), to: originalMSGType.self) originalMSG(selfObject, originalCMD)
// ... } return unsafeBitCast(swizzleIMP, to: AnyObject.self)
}
Declaration
Objective-C
+ (BOOL)fw_swizzleInstanceMethod:(nonnull Class)originalClass selector:(nonnull SEL)originalSelector withBlock: (nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
originalClass
原始类
originalSelector
原始方法
block
实现句柄
Return Value
是否成功
-
使用swizzle替换类静态方法为block实现。复杂情况不会冲突,推荐使用
Declaration
Objective-C
+ (BOOL)fw_swizzleClassMethod:(nonnull Class)originalClass selector:(nonnull SEL)originalSelector withBlock: (nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
originalClass
原始类
originalSelector
原始方法
block
实现句柄
Return Value
是否成功
-
使用swizzle替换类实例方法为block实现,identifier相同时仅执行一次。复杂情况不会冲突,推荐使用
Declaration
Objective-C
+ (BOOL)fw_swizzleInstanceMethod:(nonnull Class)originalClass selector:(nonnull SEL)originalSelector identifier:(nonnull NSString *)identifier withBlock: (nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
originalClass
原始类
originalSelector
原始方法
identifier
唯一标识
block
实现句柄
Return Value
是否成功
-
使用swizzle替换类静态方法为block实现,identifier相同时仅执行一次。复杂情况不会冲突,推荐使用
Declaration
Objective-C
+ (BOOL)fw_swizzleClassMethod:(nonnull Class)originalClass selector:(nonnull SEL)originalSelector identifier:(nonnull NSString *)identifier withBlock: (nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
originalClass
原始类
originalSelector
原始方法
identifier
唯一标识
block
实现句柄
Return Value
是否成功
-
使用swizzle替换对象实例方法为block实现,identifier相同时仅执行一次。结合fwIsSwizzleMethod使用
Declaration
Objective-C
- (BOOL)fw_swizzleInstanceMethod:(nonnull SEL)originalSelector identifier:(nonnull NSString *)identifier withBlock: (nonnull id _Nonnull (^)( __unsafe_unretained Class _Nonnull, SEL _Nonnull, IMP _Nonnull (^_Nonnull __strong)(void)))block;
Parameters
originalSelector
原始方法
identifier
唯一标识
block
实现句柄
Return Value
是否成功
-
判断对象是否使用swizzle替换过指定identifier实例方法。结合fwSwizzleMethod使用
Note
因为实际替换的是类方法,为了防止影响该类其它对象,需先判断该对象是否替换过,仅替换过才执行自定义流程
Declaration
Objective-C
- (BOOL)fw_isSwizzleInstanceMethod:(nonnull SEL)originalSelector identifier:(nonnull NSString *)identifier;
Parameters
originalSelector
原始方法
identifier
唯一标识
Return Value
是否替换
-
订阅主题通知并指定主题上下文(如vc|view),非UITraitEnvironment等需指定后才能响应系统主题
Declaration
Objective-C
@property (nonatomic, weak, nullable) id<UITraitEnvironment> fw_themeContext;
-
添加iOS13主题改变通知回调,返回订阅唯一标志,需订阅后才生效
Declaration
Objective-C
- (nullable NSString *)fw_addThemeListener: (nonnull void (^)(FWThemeStyle))listener;
-
iOS13根据订阅唯一标志移除主题通知回调
Declaration
Objective-C
- (void)fw_removeThemeListener:(nullable NSString *)identifier;
-
iOS13移除所有主题通知回调,一般用于cell重用
Declaration
Objective-C
- (void)fw_removeAllThemeListeners;
-
iOS13主题改变包装器钩子,如果父类有重写,记得调用super,需订阅后才生效
Declaration
Objective-C
- (void)fw_themeChanged:(FWThemeStyle)style;