I understand the runtime mechanism in iOS (1)

Source: Internet
Author: User

first of all, we have to first know that object-c is a compiler, dynamic language (here is the emphasis on OC is a static type of language), which in the development of the language is and many, the general dynamic language is mostly interpreted language. OC is able to do this even if the compiled language is a dynamic language. is to benefit from the runtime mechanism.
As this article is mainly about the runtime mechanism, so the language type does not do too much description, then you can write another language type of introduction.


Second, here is the main explanation of how to use the runtime, the main points of knowledge are as follows:
1, class_copypropertylist get a copy of the member list array
2. Property_getname Get member name
3. Class_getinstancevariable gets the Ivar of the member object
4, Object_getivar from the Ivar object to take the value

5. Object_setivar Assignment function

Here's a look at the code implementation:

1. Create a class

CustomClass.h

#import <Foundation/Foundation.h> @interface Customclass:nsobject@property (nonatomic, Strong) NSString *age;-( void) test; @end

Customclass.m

#import "CustomClass.h" @implementation customclass-(void) test{    NSLog (@ "Here is Customclass");    } @end


Next, create a category to implement the runtime mechanism

Nsobject+runtimetest.h

#import <Foundation/Foundation.h> @interface nsobject (runtimetest)//Create a class dynamically, and then assign a value to the age property of the class. -(ID) Testruntime: (NSString *) ClassName Age: (NSString *) age; @end

Nsobject+runtimetest.m

#import "Nsobject+runtimetest.h" #import <objc/runtime.h> #import "CustomClass.h" @implementation nsobject ( Runtimetest)-(Customclass *) Testruntime: (NSString *) ClassName Age: (NSString *) age{//Propertycount number of member properties Unsigne    d int propertycount = 0;        /* objc_property_t According to the Apple documentation: an opaque type, that represents a objective-c declared property. Personal understanding: An object of any type represents an OC-declared attribute member (my English level is limited) */* Class_copypropertylist document: Return Value An array of pointers of Type objc_property_t describing the properties declared by the class. Any properties declared by Superclasses is not included. The array contains *outcount pointers followed by a NULL terminator.     You must free, the array with free ().     Personal understanding: The main 2 meaning: Return an array pointer, type objc_property_t, run out to remember free, release.    */objc_property_t *properties = Class_copypropertylist ([self class], &propertycount);        for (unsigned int i = 0; i < Propertycount; i++) {objc_property_t property = Properties[i];   Gets the name of the member     NSString *propertyname = [[NSString alloc] Initwithcstring:property_getname (property) encoding:nsutf8stringencoding]        ;                NSLog (@ "propertyname =%@-member name", PropertyName);        Gets the member content of the Ivar Ivar Ivar = class_getinstancevariable ([self class], [PropertyName utf8string]); In fact, the above line to get the code is for the sake of insurance, basically is not get content. Because the name of the member will be preceded by the "_", if (IVar = = nil) {IVar = Class_getinstancevariable ([self class], [[NSString Stringw        ithformat:@ "_%@", PropertyName] utf8string]);        }//value id propertyval = Object_getivar (self, iVar);                                       NSLog (@ "propertyval =%@-Value", propertyval);        Dynamically Create Class Varclass = nsclassfromstring (classname);        ID varobj = [[Varclass alloc] init];        Call the method of the newly created class, here to be sure to introduce the corresponding header file, otherwise the call method will be good.                [Varobj test];                                Ivar ivarobj = class_getinstancevariable (Varclass, [@ "_age" utf8string]); Object_setivar (Varobj, Ivarobj,Age);    return varobj;            } return nil; } @end

Test:

Viewcontroller.m

#import "Nsobject+runtimetest.h" #import "CustomClass.h" @interface Viewcontroller () @property (nonatomic, Strong) NSString *name; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];    Additional setup after loading the view, typically from a nib.        _name = @ "Xiaonan";    Customclass *tmp = [self testruntime:@ "customclass" age:@ "098"];    NSLog (@ "tmp.age =%@", tmp.age);        }


Result output:

2014-11-29 17:16:55.483 testruntime2[21001:176367] propertyname = name--member name 2014-11-29 17:16:55.483 TestRunTime2[ 21001:176367] Propertyval = Xiaonan-value 2014-11-29 17:16:55.484 testruntime2[21001:176367] This is customclass2014-11-29. 17:16:55.484 testruntime2[21001:176367] Tmp.age = 098


Original address: http://blog.csdn.net/qqmcy/article/details/41596725

I understand the runtime mechanism in iOS (1)

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.