How to quickly assign values to similar variables, such as variable names

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.