ios進階開發,runtime(一)

來源:互聯網
上載者:User

標籤:io   os   ar   for   strong   sp   2014   on   cti   

建立公用類:

@interface CustomClass : NSObject

- (void) fun1;

@end

 

@implementation CustomClass

-(void)fun1

{

    NSLog(@"fun1");

}

@end

 

@interface TestClass : NSObject

 

@end

 

@implementation TestClass

 

@end

 

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

 

列印結果:

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

說明:

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

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

 

object_dispose(objTest);

        [objTest release];

runtimetest(2791,0x7fff791b2310) malloc: *** error for object 0x100202950: pointer being freed was not allocated

 

3、更改對象的類/擷取對象的類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];

運行結果:

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

運行結果:

2014-10-20 10:12:34.047 runtimetest[3726:303] CustomClass

 

4、擷取對象的類名  constchar *object_getClassName(id obj)

 

CustomClass * obj = [CustomClass new];

        NSString * className =[NSString stringWithCString:object_getClassName(obj) encoding:NSUTF8StringEncoding];

        NSLog(@"%@",className);

運行結果:

2014-10-20 10:22:54.514 runtimetest[4160:303] CustomClass

5、給一個類添加方法  

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

 

}

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

2014-10-20 10:47:25.749 runtimetest[5333:303] a:10

 

 

 /**

  * 兩個參數

  *

  */

 

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

}

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.