NSObject(QMUI_DataBind)

@interface NSObject (QMUI_DataBind)

/**
 给对象绑定上另一个对象以供后续取出使用,如果 object 传入 nil 则会清除该 key 之前绑定的对象
 
 @attention 被绑定的对象会被 strong 强引用
 @note 内部是使用 objc_setAssociatedObject / objc_getAssociatedObject 来实现
 
 @code
 - (UITableViewCell *)cellForIndexPath:(NSIndexPath *)indexPath {
 // 1)在这里给 button 绑定上 indexPath 对象
 [cell qmui_bindObject:indexPath forKey:@"indexPath"];
 }
 
 - (void)didTapButton:(UIButton *)button {
 // 2)在这里取出被点击的 button 的 indexPath 对象
 NSIndexPath *indexPathTapped = [button qmui_getBoundObjectForKey:@"indexPath"];
 }
 @endcode
 */
- (void)qmui_bindObject:(nullable id)object forKey:(NSString *)key;

/**
 给对象绑定上另一个对象以供后续取出使用,但相比于 qmui_bindObject:forKey:,该方法不会 strong 强引用传入的 object
 */
- (void)qmui_bindObjectWeakly:(nullable id)object forKey:(NSString *)key;

/**
 取出之前使用 bind 方法绑定的对象
 */
- (nullable id)qmui_getBoundObjectForKey:(NSString *)key;

/**
 给对象绑定上一个 double 值以供后续取出使用
 */
- (void)qmui_bindDouble:(double)doubleValue forKey:(NSString *)key;

/**
 取出之前用 bindDouble:forKey: 绑定的值
 */
- (double)qmui_getBoundDoubleForKey:(NSString *)key;

/**
 给对象绑定上一个 BOOL 值以供后续取出使用
 */
- (void)qmui_bindBOOL:(BOOL)boolValue forKey:(NSString *)key;

/**
 取出之前用 bindBOOL:forKey: 绑定的值
 */
- (BOOL)qmui_getBoundBOOLForKey:(NSString *)key;

/**
 给对象绑定上一个 long 值以供后续取出使用
 */
- (void)qmui_bindLong:(long)longValue forKey:(NSString *)key;

/**
 取出之前用 bindLong:forKey: 绑定的值
 */
- (long)qmui_getBoundLongForKey:(NSString *)key;

/**
 移除之前使用 bind 方法绑定的对象
 */
- (void)qmui_clearBindingForKey:(NSString *)key;

/**
 移除之前使用 bind 方法绑定的所有对象
 */
- (void)qmui_clearAllBinding;

/**
 返回当前有绑定对象存在的所有的 key 的数组,如果不存在任何 key,则返回一个空数组
 @note 数组中元素的顺序是随机的
 */
- (NSArray<NSString *> *)qmui_allBindingKeys;

/**
 返回是否设置了某个 key
 */
- (BOOL)qmui_hasBindingKey:(NSString *)key;

@end

Undocumented

  • 给对象绑定上另一个对象以供后续取出使用,如果 object 传入 nil 则会清除该 key 之前绑定的对象

    @attention 被绑定的对象会被 strong 强引用

    Note

    内部是使用 objc_setAssociatedObject / objc_getAssociatedObject 来实现
    - (UITableViewCell *)cellForIndexPath:(NSIndexPath *)indexPath {
    // 1)在这里给 button 绑定上 indexPath 对象
    [cell qmui_bindObject:indexPath forKey:@"indexPath"];
    }
    
    - (void)didTapButton:(UIButton *)button {
    // 2)在这里取出被点击的 button 的 indexPath 对象
    NSIndexPath *indexPathTapped = [button qmui_getBoundObjectForKey:@"indexPath"];
    }
    

    Declaration

    Objective-C

    - (void)qmui_bindObject:(nullable id)object forKey:(nonnull NSString *)key;

    Swift

    func qmui_bindObject(_ object: Any?, forKey key: String)
  • 给对象绑定上另一个对象以供后续取出使用,但相比于 qmui_bindObject:forKey:,该方法不会 strong 强引用传入的 object

    Declaration

    Objective-C

    - (void)qmui_bindObjectWeakly:(nullable id)object
                           forKey:(nonnull NSString *)key;

    Swift

    func qmui_bindObjectWeakly(_ object: Any?, forKey key: String)
  • 取出之前使用 bind 方法绑定的对象

    Declaration

    Objective-C

    - (nullable id)qmui_getBoundObjectForKey:(nonnull NSString *)key;

    Swift

    func qmui_getBoundObject(forKey key: String) -> Any?
  • 给对象绑定上一个 double 值以供后续取出使用

    Declaration

    Objective-C

    - (void)qmui_bindDouble:(double)doubleValue forKey:(nonnull NSString *)key;

    Swift

    func qmui_bindDouble(_ doubleValue: Double, forKey key: String)
  • 取出之前用 bindDouble:forKey: 绑定的值

    Declaration

    Objective-C

    - (double)qmui_getBoundDoubleForKey:(nonnull NSString *)key;

    Swift

    func qmui_getBoundDouble(forKey key: String) -> Double
  • 给对象绑定上一个 BOOL 值以供后续取出使用

    Declaration

    Objective-C

    - (void)qmui_bindBOOL:(BOOL)boolValue forKey:(nonnull NSString *)key;

    Swift

    func qmui_bindBOOL(_ boolValue: Bool, forKey key: String)
  • 取出之前用 bindBOOL:forKey: 绑定的值

    Declaration

    Objective-C

    - (BOOL)qmui_getBoundBOOLForKey:(nonnull NSString *)key;

    Swift

    func qmui_getBoundBOOL(forKey key: String) -> Bool
  • 给对象绑定上一个 long 值以供后续取出使用

    Declaration

    Objective-C

    - (void)qmui_bindLong:(long)longValue forKey:(nonnull NSString *)key;

    Swift

    func qmui_bindLong(_ longValue: Int, forKey key: String)
  • 取出之前用 bindLong:forKey: 绑定的值

    Declaration

    Objective-C

    - (long)qmui_getBoundLongForKey:(nonnull NSString *)key;

    Swift

    func qmui_getBoundLong(forKey key: String) -> Int
  • 移除之前使用 bind 方法绑定的对象

    Declaration

    Objective-C

    - (void)qmui_clearBindingForKey:(nonnull NSString *)key;

    Swift

    func qmui_clearBinding(forKey key: String)
  • 移除之前使用 bind 方法绑定的所有对象

    Declaration

    Objective-C

    - (void)qmui_clearAllBinding;

    Swift

    func qmui_clearAllBinding()
  • 返回当前有绑定对象存在的所有的 key 的数组,如果不存在任何 key,则返回一个空数组

    Note

    数组中元素的顺序是随机的

    Declaration

    Objective-C

    - (nonnull NSArray<NSString *> *)qmui_allBindingKeys;

    Swift

    func qmui_allBindingKeys() -> [String]
  • 返回是否设置了某个 key

    Declaration

    Objective-C

    - (BOOL)qmui_hasBindingKey:(nonnull NSString *)key;

    Swift

    func qmui_hasBindingKey(_ key: String) -> Bool