iOS Runtime 運(yùn)行時(shí)機(jī)制簡(jiǎn)單用法(二)

我們先創(chuàng)建一個(gè)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)部進(jìn)行使用

//
//  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));
        //變量實(shí)例大小
        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(@"+++++++++++++++++++++++++++");
        //對(duì)象方法,減號(hào)方法
        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ù)實(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;
}

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凌唬,一起剝皮案震驚了整個(gè)濱河市并齐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌客税,老刑警劉巖况褪,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異更耻,居然都是意外死亡测垛,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門酥夭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)赐纱,“玉大人脊奋,你說(shuō)我怎么就攤上這事熬北。” “怎么了诚隙?”我有些...
    開(kāi)封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵讶隐,是天一觀的道長(zhǎng)隔崎。 經(jīng)常有香客問(wèn)我令蛉,道長(zhǎng)煞茫,這世上最難降的妖魔是什么黎茎? 我笑而不...
    開(kāi)封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮炉峰,結(jié)果婚禮上畏妖,老公的妹妹穿的比我還像新娘。我一直安慰自己疼阔,他們只是感情好戒劫,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著婆廊,像睡著了一般迅细。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上淘邻,一...
    開(kāi)封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天茵典,我揣著相機(jī)與錄音,去河邊找鬼宾舅。 笑死统阿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的筹我。 我是一名探鬼主播砂吞,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼崎溃!你這毒婦竟也來(lái)了蜻直?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤袁串,失蹤者是張志新(化名)和其女友劉穎概而,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囱修,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赎瑰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了破镰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片餐曼。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖鲜漩,靈堂內(nèi)的尸體忽然破棺而出源譬,到底是詐尸還是另有隱情,我是刑警寧澤孕似,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布踩娘,位于F島的核電站,受9級(jí)特大地震影響喉祭,放射性物質(zhì)發(fā)生泄漏养渴。R本人自食惡果不足惜雷绢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望理卑。 院中可真熱鬧翘紊,春花似錦、人聲如沸藐唠。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)中捆。三九已至鸯匹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間泄伪,已是汗流浹背殴蓬。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蟋滴,地道東北人染厅。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像津函,于是被迫代替她去往敵國(guó)和親肖粮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

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