@property does not automatically generate instance variables and access methods in category categories, so associated objects are used to add “properties” to existing classes. Use objc_getAssociatedObject and objc_setAssociatedObject to simulate the property access method

The <objc/ Runtime.h > header file needs to be imported before a category uses associated objects

#import <objc/runtime.h>
Copy the code
The associated object API provided in the OC
Set method objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key, ID _Nullable value, Objc_AssociationPolicy policy) Get method objc_getAssociatedObject(id _Nonnull object, Const void * _Nonnull key) Remove the associated object objc_removeAssociatedObjects(id _Nonnull object)Copy the code
Parameter meaning
  • object The associated object is the main branch of the object
  • key The key value is a one-to-one mapping between the key and the associated object. Must be globally unique. Commonly used in the following ways@selector(methodName)Associated method object or use& Custom KEY(__bridge const void *)(custom KEY)Bind a custom static KEY
  • value The object or value to be associated
  • policy Related policies

Objc_AssociationPolicy A policy has five associationpolicies, like modifiers such as nonatomic and strong

/**
 * Policies related to associative references.
 * These are options to objc_setAssociatedObject()
 */
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
    OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object.
                                            *   The association is made atomically. */
    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied.
                                            *   The association is made atomically. */
};
Copy the code
Association policy objc_AssociationPolicy Policy Equivalent modifier
OBJC_ASSOCIATION_ASSIGN @property(weak)
OBJC_ASSOCIATION_RETAIN_NONATOMIC @property(strong, nonatomic)
OBJC_ASSOCIATION_COPY_NONATOMIC @property(copy, nonatomic)
OBJC_ASSOCIATION_RETAIN @property(strong,atomic)
OBJC_ASSOCIATION_COPY @property(copy, atomic)

The sample code

#import <objc/runtime.h>

@implementation UITextField (LengthLimit)

static NSString *kLimitTextLengthKey = @"kLimitTextLengthKey";

- (void)setMaxLength:(int)length {
    objc_setAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey), [NSNumber numberWithInt:length], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self addTarget:self action:@selector(textFieldMaxLengthLimit:) forControlEvents:UIControlEventEditingChanged];
}



- (void)textFieldMaxLengthLimit:(id)textField {
    NSNumber *lengthNumber = objc_getAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey));
    int maxLength = [lengthNumber intValue];
}
Copy the code

_cmd related

_cmd represents the selector of the current method in objective-C methods, just as self represents the object instance of the current method call.

CMD is only scoped in the current method, which refers to the current method name @selector

#import "NSObject+AssociatedObject.h"
#import <objc/runtime.h>

@implementation NSObject (AssociatedObject)

- (void)setAssociatedObject:(id)associatedObject {
    objc_setAssociatedObject(self.@selector(associatedObject), associatedObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)associatedObject {
    return objc_getAssociatedObject(self.@selector(associatedObject)); } _cmd is used instead of _cmd- (id)associatedObject {
    return objc_getAssociatedObject(self, _cmd);
}

@end
Copy the code