How to quickly assign values to similar variables, such as variable names
Now there is a requirement that there are four variables in the controller and the same value needs to be conveniently assigned. How can this be achieved?
{NSArray * _ arr1; NSArray * _ arr2; NSArray * _ arr3; NSArray * _ arr4 ;}
I used the Object-C runtime to solve this problem.
For (int I = 1; I <5; I ++) {// obtain the variable. The first parameter is the class to which the variable belongs, and the second parameter is the variable name string, the string Ivar var = class_getInstanceVariable ([self class], [[NSString stringWithFormat: @ "_ arr % d", I] UTF8String]) to be converted to C. // assign a value to the variable. The first parameter is the class object to which the variable belongs, the second parameter is the variable to be assigned, and the third parameter is the assigned value object_setIvar (self, var, @ [@ "a", @ "B"]);}
Print down
14:56:19. 362 Runtime [23492: 599694] arr1 = (a, B) 14:56:19. 363 Runtime [23492: 599694] arr2 = (a, B) 14:56:19. 363 Runtime [23492: 599694] arr3 = (a, B) 14:56:19. 363 Runtime [23492: 599694] arr4 = (a, B)
Assigned successfully!