Runtime usage: (The following code is all based on an object created by the Dog class: Dog)
1. Use the ID type to create variables to accept different types of objects
2. Use [Person Performselector: @selector (test2:) withobject:@"incoming parameters"]; To invoke the method dynamically;
3. Use Objc_setassociatedobject (id object,const void *key,id value,objc_associationpolicy Policy) to add additional storage space for object objects.
4. Dynamic Copy Object
Use in MRC mode:
Dog *dog = [Dognew];
Dog.name = @ "1234";
Dog.age = 3;
Dog *copydog = object_copy (dog,sizeof (dog));
5. Dynamically set the attribution class of the object Object_setclass or get the object's attribution class
Object_setclass such as the following code:
Dog *dog = [Dognew];
NSLog (@ "dog.class==%@", [Dogclass]);
NSLog (@ "%@", Object_setclass (dog, [Catclass])); The return value of the Object_setclass is the class to which the object originally belongs
NSLog (@ "dog.class==%@", [Dogclass]);
Will output:
Dog.class==dog
Dog
Dog.class==cat
Object_getclass the following code:
Dog *dog = [Dognew];
Class class = Object_getclass (dog);
NSLog (@ "%@", Class);
Will output: Dog
Which is the dynamic acquisition of the class of the dog object.
5.respondsToSelector and Performselector
Respondstoselector, which is used to determine whether an object can call the corresponding method, passing in a value of the SEL type
Performselector is used to directly use the object invocation method, passing in a parameter of the SEL type, often combining the two, an example is as follows:
if ([Dog Respondstoselector: @selector (Funofdog)]) {
[Dog Performselector: @selector (Funofdog)];
}else if ([Dogrespondstoselector: @selector (funofcat)]) {
[Dog Performselector: @selector (Funofcat)];
}
The dog class can be judged by the corresponding method and call it directly.
6. Use Object_getclassname to get the class name, just the format of the c+ string
const char * className =object_getclassname (dog);
printf ("%s", className);
Output: Dog
7. Adding a method to an object
Add a private method for the dog class, because this method is dynamically added, so you can use Respondstoselector to invoke this method dynamically, otherwise, the compilation is not passed, because in the class being added, the method is not implemented or declared.
Class_addmethod ([Dogclass], @selector (dogaddfun:str2:), (IMP) Funofadd, "[Email protected]:@@");
if ([Dog Respondstoselector: @selector (dogaddfun:str2:)]) {
int number = [Dogperformselector: @selector (dogaddfun:str2:) withobject:@ "1234" withobject:@ "5678"];
NSLog (@ "%d", number);
}else
NSLog (@ "Method not added successfully");
Funofadd method Definition:
int Funofadd (idself,sel_cmd,nsstring *str,nsstring* str2) {
return (int) (str.length + str2.length);
}//Returns the sum of two string lengths plus
8. Get all the methods of a class Class_copymethodlist
U_int count;
Method *methods = Class_copymethodlist ([Dog class], &count);
for (int i = 0; i < count; i + +) {
SEL name = Method_getname (Methods[i]);
printf ("%s\n", Sel_getname (name));
}
Wherein, the class_copymethodlist two parameters for the class name and the number of statistical methods of the unsigned shape of the variable address, whose return value is an array containing all the Objc_method methods of the class
Method_getname passed in the parameter is the Objc_method object in the class, the return value of the method corresponding to the SEL, its implementation in the runtime source code is as follows:
SEL Method_getname (method M)
{
if (!m) return NULL;
Return Oldmethod (m)->method_name;
}
Sel_getname (name) obtains the method name of the SEL through the SEL
9. Get all the property names for a class
U_int count;
objc_property_t* properites = Class_copypropertylist ([Dog class], &count);
for (int i = 0; I < Count;i + +) {
Const char* name= property_getname (Properites[i]);
printf ("%s\n", name);
}
Class_copypropertylist returns an array containing all the properties of the class, Property_getname obtains the string for the objc_property_t corresponding property name.
10. Substitution of System class methods
The implementation of two methods can be interchanged, but there is no sense of purpose, it is estimated that there is no need to meet the problem
Method method1 = Class_getinstancemethod ([NSString class], @selector (lowercasestring));
Method method2 = Class_getinstancemethod ([NSString class], @selector (uppercasestring));
Method_exchangeimplementations (Method1, METHOD2);
NSLog (@ "%@", [@ "AAAAAA" lowercasestring]);
NSLog (@ "%@", [@ "BBBBB" uppercasestring]);
11. Implementing the replacement of a custom method
Method method1 = Class_getinstancemethod ([Dog class], @selector (Funofdog));
Method Funmethod = Class_getinstancemethod ([self class], @selector (Replasefun));
Imp imp = method_getimplementation (Funmethod);
Method_setimplementation (METHOD1, IMP);
[Dog Funofdog];
Funofdog is a method defined in the dog class, Replasefun is the method defined in the call controller, two methods have only one output statement, after the execution of the above statement, the dog call Funofdog executes the statement output in this controller.
IOS Runtime Application Example