Forward iOS Advanced Development ~runtime (i)

Source: Internet
Author: User

iOS Advanced Development ~runtime (i)

iOS Advanced Development ~runtime (II)

iOS Advanced Development ~runtime (III)

iOS Advanced Development ~runtime (IV)

Some common classes:

@interface Customclass: nsobject

-(void) fun1;

@end


@implementation Customclass

-(void) fun1

{

NSLog (@ "fun1");

}

@end

@interface TestClass: nsobject

@end

@implementation TestClass

@end

Don't forget to introduce libraries:#include<objc/runtime.h>


1. Object copy:ID object_copy (ID obj, size_t size)

-(void) Copyobj

{

Customclass *obj = [Customclassnew];

NSLog (@ "%p", &obj);

id objtest = object_copy (obj,sizeof (obj));

NSLog (@ "%p", &objtest);


[Objtest fun1];

}

Printing results:

2013-07-26 15:35:11.547 Highoc[6859:c07] 0xbfffdf64

2013-07-26 15:35:11.547 Highoc[6859:c07] 0xbfffdf60

2013-07-26 15:35:11.547 Highoc[6859:c07] Fun1

Description

The Object_copy function implements a copy of the object.

2. Object Release ID object_dispose (ID obj)


-(void) Objectdispose

{

Customclass *obj = [Customclassnew];

Object_dispose(obj);

[obj release];

[obj fun1];

}

Printing results: Program crash

malloc: * * * error for Object 0x758e6d0: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)

-(void) Setclasstest

{

Customclass *obj = [Customclassnew];

[obj fun1];

Class aclass =object_setclass (obj, [customclassotherClass]);

the class of the Obj object has been changed swap the ISA to an Isa

NSLog(@ "aclass:%@", Nsstringfromclass(AClass));

NSLog(@ "obj class:%@", Nsstringfromclass([objclass]));

[obj fun2];

}

-(void) Getclasstest

{

Customclass *obj = [Customclassnew];

Class alogclass =object_getclass (obj);

NSLog(@ "%@", Nsstringfromclass(Alogclass));

}


4. Get the class name of the object ConstChar *object_getclassname (ID obj)


*obj = [CustomclassNew];

NSString *classname = [NSStringstringwithcstring: Object_getclassname(obj) encoding: Nsutf8stringencoding];

NSLog (@ "classname:%@", className);

}


BOOL Class_addmethod (class cls,SEL name,imp imp,

Const Char *types)



A 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);

}


/**

* 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);

}


Related Documents and instructions:

The Obj-c methodis a C function that requires at least two parameters (self,_cmd)

IMP is a bit like a function pointer, pointing to a concrete method implementation.

Adding a method dynamically to a class


BOOL Class_addmethod (class cls, SEL name, IMP imp, const char *types)

Parameter description:

CLS: The class of the method being added

Name: Can be understood as the method name

IMP: A function that implements this method

Types: A string that defines the return value type and parameter type of the function

Class_addmethod ([TestClass class], @selector (Ocmethod:), (IMP) TestFunc, "[Email protected]:@");

Where the types parameter is "[Email protected]:@", in order to indicate:

I: Return value type int, if v means void

@: Parameter ID (self)

:: SEL (_cmd)

@: ID (str)

Official documents:

https://developer.apple.com/library/mac/#documentation/cocoa/reference/objcruntimeref/reference/reference.html

https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objcruntimeguide/articles/ Ocrttypeencodings.html




IOS Advanced Development ~Runtime(i)

http://blog.csdn.net/zfpp25_/article/details/9496705

IOS Advanced Development ~Runtime(ii)

http://blog.csdn.net/zfpp25_/article/details/9497187

IOS Advanced Development ~Runtime(iii)

http://blog.csdn.net/zfpp25_/article/details/9497721

IOS Advanced Development ~Runtime(iv)

http://blog.csdn.net/zfpp25_/article/details/9498233

Forward iOS Advanced Development ~runtime (i)

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.