To create a public class:
@interface Customclass:nsobject
-(void) fun1;
@end
@implementation Customclass
-(void) fun1
{
NSLog (@ "fun1");
}
@end
@interface Testclass:nsobject
@end
@implementation TestClass
@end
1. Object copy:ID object_copy (ID obj, size_t size)
Customclass * obj = [Customclass new];
NSLog (@ "%p", &obj);
ID objtest = object_copy (obj, sizeof (obj));
NSLog (@ "%p", &objtest);
[Objtest FUN1];
Printing results:
2014-10-20 09:51:48.973 runtimetest[2573:303] 0x7fff5fbff818
2014-10-20 09:51:48.983 runtimetest[2573:303] 0x7fff5fbff810
2014-10-20 09:51:48.984 runtimetest[2573:303] Fun1
Description
The Object_copy function implements a copy of the object.
2. Object Release ID object_dispose (ID obj)
Object_dispose (objtest);
[Objtest release];
Runtimetest (2791,0x7fff791b2310) malloc: * * * error for Object 0x100202950:pointer being freed is not allocated
3. Change the class of object/Get object class Class Object_setclass (ID obj, class CLS)/ class Object_getclass (ID obj)
Customclass * obj = [Customclass new];
[obj fun1];
Class aclass = Object_setclass (obj, [Customclassother class]);
NSLog (@ "aclass:%@", Nsstringfromclass (AClass));
NSLog (@ "obj class:%@", Nsstringfromclass ([obj class]));
[obj fun2];
Operation Result:
2014-10-20 10:09:24.619 runtimetest[3536:303] Aclass:customclass
2014-10-20 10:09:24.619 runtimetest[3536:303] obj class:customclassother
2014-10-20 10:09:24.620 runtimetest[3536:303] Fun2
Customclass * obj = [Customclass new];
Class objclass = Object_getclass (obj);
NSLog (@ "%@", Nsstringfromclass (objclass));
Operation Result:
2014-10-20 10:12:34.047 runtimetest[3726:303] Customclass
4. Get the class name of the object ConstChar *object_getclassname (ID obj)
Customclass * obj = [Customclass new];
NSString * className =[nsstring stringwithcstring:object_getclassname (obj) encoding:nsutf8stringencoding];
NSLog (@ "%@", className);
Operation Result:
2014-10-20 10:22:54.514 runtimetest[4160:303] Customclass
5. Add a method to a class
BOOL Class_addmethod (class cls,SEL name,imp imp,
Const Char *types)
/**
* one parameter
*
*/
int cfunction (ID self , SEL _cmd, nsstring *str) {
NSLog (@ "%@", str);
return;// Returns a value randomly
}
-(void) Oneparam {
TestClass *instance = [[testclassalloc]init];
method to add
Class_addmethod ([TestClassclass],@selector (Ocmethod:), (IMP)cfunction,"[Email protected]:@ ");
if ([instance respondstoselector:@selector (Ocmethod:)]) {
NSLog(@ "Yes, instance respondstoselector: @selector (ocmethod:)");
} Else
{
NSLog (@ "Sorry");
}
int a = (int) [InstanceOcmethod:@ " I am an OC method,C function Implementation "];
NSLog (@ "a:%d", a);
}
2014-10-20 10:47:25.738 runtimetest[5333:303] Yes, instance respondstoselector: @selector (ocmethod:)
2014-10-20 10:47:25.748 runtimetest[5333:303] I was a OC of the Method , C function Implementation
2014-10-20 10:47:25.749 runtimetest[5333:303] A:10
/**
* two parameters
*
*/
int Cfunctiona (ID self , SEL _cmd, nsstring *str, nsstring *str1) {
NSLog (@ "%@-%@", str, str1);
return a value
}
-(void) Twoparam {
TestClass *instance = [[testclassalloc]init];
Class_addmethod ([TestClassclass],@selector (Ocmethoda::), (IMP)Cfunctiona,"[Email protected]:@@ ");
if ([instance respondstoselector:@selector (Ocmethoda::)]) {
NSLog(@ "Yes, instance respondstoselector: @selector (ocmethoda::)");
} Else
{
NSLog (@ "Sorry");
}
int a = (int) [instanceOcmethoda: @ " I am an OC method, C function Implementation" : @ "----- I am the second parameter "];
NSLog (@ "a:%d", a);
}
iOS advanced development, Runtime (i)