(轉寄)IOS進階開發~Runtime(一)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

IOS進階開發~Runtime(一)

IOS進階開發~Runtime(二)

IOS進階開發~Runtime(三)

IOS進階開發~Runtime(四)

一些公用類:

@interface CustomClass : NSObject

- (void) fun1;

@end


@implementation CustomClass

- (void) fun1

{

    NSLog(@"fun1");

}

@end

 

@interface TestClass : NSObject

@end

@implementation TestClass

@end

 

別忘記引入庫:#include<objc/runtime.h>


1、對象拷貝: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];

}

列印結果:

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

 

說明:

object_copy 函數實現了對象的拷貝。

2、對象釋放 id object_dispose(id obj)


- (void) objectDispose

{

    CustomClass *obj = [CustomClassnew];

    object_dispose(obj);

    

    [obj release];

    [obj fun1];

}

列印結果:程式crash

malloc: *** error for object 0x758e6d0: pointer being freed was not allocated

 

3、更改對象的類/擷取對象的類  

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

    //obj 對象的類被更改了    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、擷取對象的類名  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)



一個參數

  *

  */


int cfunction(id self, SEL _cmd, NSString *str) {

    NSLog(@"%@", str);

    return10;//隨便返回個值

}


- (void) oneParam {

    

    TestClass *instance = [[TestClassalloc]init];

    //    方法添加

    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:@"我是一個OC的method,C函數實現"];

    NSLog(@"a:%d", a);

}


 

 /**

  * 兩個參數

  *

  */


int cfunctionA(id self, SEL _cmd, NSString *str, NSString *str1) {

    NSLog(@"%@-%@", str, str1);

    return20;//隨便返回個值

}


- (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:@"我是一個OC的method,C函數實現" :@"-----我是第二個參數"];

    NSLog(@"a:%d", a);

}


相關文檔及說明:

 

Obj-C的方法(method)就是一個至少需要兩個參數(self,_cmd)的C函數

 

 IMP有點類似函數指標,指向具體的Method實現。

 

 

 向一個類動態添加方法


 BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

 參數說明:

 cls:被添加方法的類

 name:可以理解為方法名

 imp:實現這個方法的函數

 types:一個定義該函數傳回值類型和參數類型的字串

 

 

 

 class_addMethod([TestClass class], @selector(ocMethod:), (IMP)testFunc, "[email protected]:@");

 

 其中types參數為"[email protected]:@“,按順序分別表示:

 i:傳回值類型int,若是v則表示void

 @:參數id(self)

 ::SEL(_cmd)

 @:id(str)

 

 

 官方文檔:

 

 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進階開發~Runtime(一)

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

IOS進階開發~Runtime(二)

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

IOS進階開發~Runtime(三)

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

IOS進階開發~Runtime(四)

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

(轉寄)IOS進階開發~Runtime(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.