iOS Advanced Development ~runtime (II)

Source: Internet
Author: User

6. Get all the methods of a class:

U_int count;

Method * methods = Class_copymethodlist ([Customclass class], &count);

for (int i = 0; i < count; i++) {

SEL name = Method_getname (Methods[i]);

NSString * StrName = [nsstring stringwithcstring:sel_getname (name) encoding:nsutf8stringencoding];

NSLog (@ "%@", strName);

}

Operation Result:

2014-10-20 11:57:27.884 runtimetest[10939:303] Fun1

2014-10-20 11:57:27.886 runtimetest[10939:303] Fun3

2014-10-20 11:57:27.886 runtimetest[10939:303] VarTest1

2014-10-20 11:57:27.886 runtimetest[10939:303] SetVarTest1:

2014-10-20 11:57:27.887 runtimetest[10939:303] VarTest2

2014-10-20 11:57:27.887 runtimetest[10939:303] SetVarTest2:

2014-10-20 11:57:27.887 runtimetest[10939:303] VarTest3

2014-10-20 11:57:27.888 runtimetest[10939:303] SetVarTest3:

7

Get all properties of a class

U_int count;

objc_property_t * Properties = Class_copypropertylist ([Customclass class], &count);

for (int i = 0; i < count; i++) {

const char * propertyname = Property_getname (Properties[i]);

NSString * StrName = [NSString stringwithcstring:propertyname encoding:nsutf8stringencoding];

NSLog (@ "%@", strName);

}

Operation Result:

2014-10-20 12:01:23.508 runtimetest[11167:303] VarTest1

2014-10-20 12:01:23.510 runtimetest[11167:303] VarTest2

2014-10-20 12:01:23.510 runtimetest[11167:303] VarTest3

8. Get/Set property variables for class

Gets the value of the global variable (myfloat is a property variable for the class)

-(void) Getinstancevar {

float Myfloatvalue;

object_getinstancevariable (self,"Myfloat", (void*) &myfloatvalue);

NSLog (@ "%f", Myfloatvalue);

}

Set the value of a global variable

-(void) Setinstancevar {

float newvalue = 10.00f;

unsigned int addr = (unsignedint) &newValue;

Object_setinstancevariable(self,"Myfloat", * (float*) addr);

NSLog (@ "%f", myfloat);

}

9. Determine the type of a property of a class

-(void) Getvartype {

Classcustomclass *obj = [Classcustomclassnew];

Ivar var = class_getinstancevariable(object_getclass(obj),"VarTest1");

Const char* typeencoding =ivar_gettypeencoding (var);

NSString *stringtype = [NSStringstringwithcstring: typeencodingencoding: nsutf8stringencoding ];

if ([StringType hasprefix:@ "@"]) {

Handle class case

NSLog(@ "Handle class case");

} Else if ([StringTypehasprefix:@ "I"]) {

handle INT Case

NSLog(@ "Handle int case");

} Else if ([StringTypehasprefix:@ "F"]) {

Handle float case

NSLog(@ "Handle float case");

} Else

{

}

}

10. Get the name of the property by its value (reflection mechanism)

-(NSString *) Nameofinstance: (ID) instance

{

unsigned int numivars =0;

nsstring *key=nil;

Describes the instance variables declared by a class.

Ivar * Ivars = class_copyivarlist ([Classcustomclassclass], &numivars);

For (int i = 0; i < Numivars; i++) {

Ivar Thisivar = ivars[i];

const Char *type =ivar_gettypeencoding (thisivar);

NSString *stringtype = [NSStringstringwithcstring: typeencoding: nsutf8stringencoding];

It's not class , it's skipped.

if (![ StringType Hasprefix:@ "@"]) {

continue;

}

Reads the value of an instance variable in an object. Object_getivar This method, when a non-Objective-c object is encountered and is directly crash

if ((object_getivar (allobj, thisivar) = = instance)) {

Returns the name of an instance variable.

key = [NSStringstringwithutf8string: Ivar_getname(Thisivar)];

Break ;

}

}

Free (ivars);

return key;

}

Test code:

AllObj = [Classcustomclassnew];

allobj. VarTest1 [email protected] "vartest1string";

allobj. VarTest2 [email protected] "vartest2string";

allobj. VARTEST3 [email protected] "vartest3string";

nsstring *str = [selfnameofinstance:@ "vartest1string"];

NSLog (@ "str:%@", str);

Printing results:

2013-07-26 16:26:26.271 Highoc[7081:c07] Str:vartest1

iOS Advanced Development ~runtime (II)

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.