iOS动态添加和获取属性

我们可以利用

objc_setAssociatedObject

objc_getAssociatedObject

来为类动态添加和获取属性



static char dynamicKey;
    NSArray *array = @[@"4", @"5", @"6"];
    NSString *dymaicString = @"动态添加属性";
    objc_setAssociatedObject(array, &dynamicKey, dymaicString, OBJC_ASSOCIATION_RETAIN);
    
    NSString *associatedObject = (NSString *)objc_getAssociatedObject(array, &dynamicKey);
    NSLog(@"%@",associatedObject);


你可能感兴趣的:(iOS)