The copyright of this article belongs to the public account “an old code farmer”.

Add attributes to runtime

In Objective-C, category categories can only add methods, not attributes, by default. The root reason is that a category does not automatically generate set and GET methods once you declare @property. If you need to add attributes to a category, you can use the Runtime features to do so.

// Create a new category class for NSObject, And add a customString property @Interface NSObject (Category) @Property (nonatomic,copy)NSString *customString; @end // Implement the set and get methods in the.m file. #import "NSObject+ category.h "#import <objc/message.h> - (void)setCustomString:(NSString *)customString { objc_setAssociatedObject(self, &customStringKey, customString, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)customString { return objc_getAssociatedObject(self, &customStringKey); } - (void)viewDidLoad {[super viewDidLoad]; Objct = [[NSObject alloc] init]; objct.customString = @"1111"; NSLog(@"%@",objct.customString); }Copy the code

To dynamically add attributes, two runtime functions are used:

1. Add attributes

This function takes four arguments. The first parameter refers to the object to which the attribute is to be added, the second parameter refers to the key pointer to the attribute, the third parameter refers to the name of the attribute, and the fourth parameter specifies the type and atomicity. The fourth parameter has five values:

  1. OBJC_ASSOCIATION_ASSIGN generates a weakly-typed property equivalent to @property(atomic,assign)
  2. OBJC_ASSOCIATION_RETAIN_NONATOMIC equivalent to @property(nonatomic,strong)
  3. OBJC_ASSOCIATION_COPY_NONATOMIC, equivalent to @property(nonatomic,copy)
  4. OBJC_ASSOCIATION_RETAIN, equivalent to @property(atomic,strong)
  5. OBJC_ASSOCIATION_COPY, equivalent to @property(atomic,copy)

The above code generates a string, so we use OBJC_ASSOCIATION_COPY_NONATOMIC instead

2. Obtain the attribute value

The second function gets the dynamically generated property. This function takes two arguments, the first referring to the property of which object and the second as the key pointer to the property (each dynamically added property requires a unique key).

Runtime dynamic add method

Add a method to the People class. If “singing” is printed after running, the method is successfully added

Void viewDidLoad {[super viewDidLoad]; void viewDidLoad {[super viewDidLoad]; People *people = [[People alloc] init]; // Add method class_addMethod([People class], @selector(sing), class_getMethodImplementation([self class], @selector(peopleSing)), "v@:"); //people calls the new method [people performSelector:@selector(sing)]; } - (void)peopleSing {NSLog(@" singing "); }Copy the code

The add method uses two runtime functions

1. Add method functions

This function takes four arguments. The first parameter represents which class to add method, the second parameter represents the name of the added method, the third parameter has been implemented method IMP pointer, the fourth parameter “v@:” :” v: “means to add method return value void; The “@” indicates the id type (that is, the class to be added); “:” indicates the added SEL as shown below:

2. Obtain method IMP pointer

This function gets the IMP pointer as an argument to the first function, which takes two arguments. The first parameter is the class in which the method is implemented. The second parameter is the SEL of the implemented method

This article is first published in the public number [an old code farming], pay attention to the public number free access to learning video

IOS Runtime — Dynamically adding properties and methods