iOS Runtime 運行時機制簡單用法(二)

我們先創(chuàng)建一個MyClass類,其代碼如下,用法都在代碼中,有注釋

MyClass.h 文件

//
//  MyClass.h
//  RuntimeC++
//
//  Created by plee on 15/10/8.
//  Copyright ? 2015年 franklee. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyClass : NSObject<NSCopying,NSCoding>

@property (nonatomic,strong) NSArray * array;
@property (nonatomic,copy) NSString * string;

- (void)method1;
- (void)method2;
+ (void)classMethod1;
- (void)method3WithArg1:(NSInteger )arg1 arg2:(NSString * )string;
- (void)setdataWith:(NSDictionary *)dict;

@end

MyClass.m 文件如下

//
//  MyClass.m
//  RuntimeC++
//
//  Created by plee on 15/10/8.
//  Copyright ? 2015年 franklee. All rights reserved.
//

#import "MyClass.h"
@interface MyClass(){
    NSInteger _instance1;
    NSString * _instance2;
    
}
@property (nonatomic,assign) NSUInteger integer;


@end

@implementation MyClass
- (void)method1{
    NSLog(@"call method method1");
    
}
- (void)method2{
    
}
+ (void)classMethod1{
    
}


- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)string{
    NSLog(@"arg1 : %ld,arg2 : %@",arg1,string);
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
    
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    return self;
    
}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}
- (void)setdataWith:(NSDictionary *)dict{
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (key ) {
            objc_property_t property = class_getProperty([self class], [key UTF8String]);
            NSString * attriString = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
            NSLog(@"%@",attriString);
            [self setValue:obj forKey:key];
        }
    }];
}
@end

在main函數(shù)內(nèi)部進行使用

//
//  main.m
//  RuntimeC++
//
//  Created by plee on 15/10/8.
//  Copyright ? 2015年 franklee. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "MyClass.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        
        NSLog(@"Hello, World!");
        MyClass * myCalss = [[MyClass alloc] init];
        unsigned int outCount = 0;
        Class cls = myCalss.class;
        //類名
        NSLog(@"class name : %s",class_getName(cls));
        //父類
        NSLog(@"super class name :%s",class_getName(class_getSuperclass(cls)));
        NSLog(@"=================================");
        //是否是元類
        NSLog(@"MyClass is %@ a meta-class",(class_isMetaClass(cls))?@"yes":@"not");
        
        Class meta_class = objc_getMetaClass(class_getName(cls));
        NSLog(@"%s's meta-class is %s",class_getName(cls),class_getName(meta_class));
        //變量實例大小
        NSLog(@"instance size %zu",class_getInstanceSize(cls));
        NSLog(@"------------------------------------");
        
        //成員變量
        Ivar * ivars = class_copyIvarList(cls, &outCount);
        for (int i=0; i<outCount; i++) {
            Ivar ivar = ivars[i];
            NSLog(@"instance variable's name :%s at index : %d",ivar_getName(ivar),i);
            
        }
        free(ivars);
        Ivar string = class_getInstanceVariable(cls, "_string");
        if (string !=NULL) {
            NSLog(@"instance variable %s",ivar_getName(string));
            
        }
        NSLog(@"-------------------------");
        //屬性操作
        objc_property_t array = class_getProperty(cls, "array");
        if (array !=NULL) {
            NSLog(@"property is : %s ",property_getName(array));
        }
         NSLog(@"-------------------------++++++++++++");
       //方法操作
        Method * methods = class_copyMethodList(cls, &outCount);
        for (int j=0; j<outCount; j++) {
            Method method1 = methods[j];
            SEL method_name = method_getName(method1);
            NSString * strName = [NSString stringWithCString:sel_getName(method_name) encoding:NSUTF8StringEncoding];
            
            NSLog(@"method's signature :%@",strName);
            
        }
        free(methods);
        NSLog(@"+++++++++++++++++++++++++++");
        //對象方法,減號方法
        Method method1 = class_getInstanceMethod(cls, @selector(method1));
        if (method1!=NULL) {
            SEL method_name = method_getName(method1);
            NSString * strName = [NSString stringWithCString:sel_getName(method_name) encoding:NSUTF8StringEncoding];
            NSLog(@"class  method :%@",strName);
            
        }
        
        
        //類方法class_getClassMethod
        Method classMethod = class_getClassMethod(cls, @selector(classMethod1));
        if (classMethod!=NULL) {
            SEL method_name1 = method_getName(classMethod);
            NSString * str = [NSString stringWithCString:sel_getName(method_name1) encoding:NSUTF8StringEncoding];
            NSLog(@"class method id :%@",str);
            
        }
        NSLog(@"MyClass is %@ responced to selector :method3WithArg1:Arg2:",(class_respondsToSelector(cls, @selector(method3WithArg1:arg2:))?@"yes":@"not"));
        //函數(shù)實現(xiàn)指針
        IMP imp = class_getMethodImplementation(cls, @selector(method1));
        imp();
        NSLog(@"=========================");
        //協(xié)議
        Protocol * __unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount);
        Protocol * protocol;
        for (int i=0; i<outCount; i++) {
            protocol  = protocols[i];
            NSLog(@"protocol name : %s",protocol_getName(protocol));
            
        }
        NSLog(@"MyClass is %@ responsed to protocol %s ",(class_conformsToProtocol(cls, protocol)?@"yes":@"not"),protocol_getName(protocol));
        int numClass ;
        Class * classes = NULL;
        numClass = objc_getClassList(NULL, 0);
        if (numClass>0) {
            classes =(Class *) malloc(sizeof(Class)  * numClass);
            numClass  = objc_getClassList(classes, numClass);
            NSLog(@"number of clesses :%d",numClass);
            for (int i=0; i<numClass; i++) {
                Class clsname = classes[i];
                NSLog(@"class name :%s",class_getName(clsname));
                
            }
            free(classes);
            
        }
        float a[] = {1.0,2.0,3.0,4.0};
        NSLog(@"array encoding type :%s",@encode(typeof(a)));
        
        
    }
    return 0;
}

以上都是對簡單runtime函數(shù)進行的簡單學(xué)習(xí)和使用,有什么新的見解都可以留言,我也只是簡單的了解,未做深入的探討

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,400評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,136評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,452評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,818評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,997評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,552評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,292評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,510評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,721評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,235評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,480評論 2 379

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,825評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,722評論 25 708
  • 原文出處:南峰子的技術(shù)博客 Objective-C語言是一門動態(tài)語言,它將很多靜態(tài)語言在編譯和鏈接時期做的事放到了...
    _燴面_閱讀 1,249評論 1 5
  • 本文轉(zhuǎn)載自:http://southpeak.github.io/2014/10/25/objective-c-r...
    idiot_lin閱讀 948評論 0 4
  • 有一個男孩,他的爸爸是一個部門干部,孩子從小家庭經(jīng)濟情況比較優(yōu)越,又是家中獨苗,父母比較嬌慣,加上親戚及父母的同事...
    長跑人閱讀 289評論 0 2