国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Runtime的介紹與簡單運(yùn)用(一)

2019-11-09 18:32:46
字體:
供稿:網(wǎng)友

runtime簡稱運(yùn)行時

        我們都知道Object-C是一種是根據(jù)C語言所衍生出來的語言,因此我們的代碼在程序運(yùn)行過程中都會被轉(zhuǎn)化成C代碼執(zhí)行,而runtime就相當(dāng)于這個橋梁,對于一個想要真正理解OC語言的人,學(xué)習(xí)runtime是必不可少的,好比想要深刻理解java,映射是必不可少一樣。

例如[objc logMyInfo];會被轉(zhuǎn)化成objc_msgSend(objc, @selector(logMyInfo));。

OC中一切都被設(shè)計成了對象,我們都知道一個類被初始化成一個實(shí)例,這個實(shí)例是一個對象。實(shí)際上一個類本質(zhì)上也是一個對象,在runtime中用結(jié)構(gòu)體表示。

例如導(dǎo)入#import <objc/runtime.h>我們可以從runtime中看到

/// An opaque type that rePResents a method in a class definition.描述類中的一個方法typedef struct objc_method *Method;/// An opaque type that represents an instance variable.實(shí)例變量typedef struct objc_ivar *Ivar;/// An opaque type that represents a category. 類別Categorytypedef struct objc_category *Category;/// An opaque type that represents an Objective-C declared property.類中聲明的屬性typedef struct objc_property *objc_property_t;//類在runtime中的表示struct objc_class {    Class isa  OBJC_ISA_AVAILABILITY;    //指針,顧名思義,表示是一個什么,    //實(shí)例的isa指向類對象,類對象的isa指向元類#if !__OBJC2__    //父類    Class super_class                                        OBJC2_UNAVAILABLE;    //類名    const char *name                                         OBJC2_UNAVAILABLE;    long version                                             OBJC2_UNAVAILABLE;    long info                                                OBJC2_UNAVAILABLE;    long instance_size                                       OBJC2_UNAVAILABLE;    //成員變量列表    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;    //方法列表    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;    //緩存  一種優(yōu)化,調(diào)用過的方法存入緩存列表,下次調(diào)用先找緩存    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;    //協(xié)議列表    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;#endif    } OBJC2_UNAVAILABLE;

實(shí)戰(zhàn)應(yīng)用,獲取類屬性列表

1、首先我們先新建一個工程,然后創(chuàng)建一個測試類文件TestClass.h

2、然后新建一些測試類屬性,代碼如下

h文件

#import <Foundation/Foundation.h>@protocol TestDelegate <NSObject>- (void)testDelegateMethod;@end@interface TestClass : NSObject@property (nonatomic,weak) id<TestDelegate> delegate;@property (nonatomic,strong) NSString *outPrOne;- (void)thisTestMethod;@end/** 測試代理 */@protocol TestDelegateTwo <NSObject>- (void)testDelegateMethodTwo;@end@interface TestClassTwo : NSObject@property (nonatomic,weak) id<TestDelegateTwo> delegateTwo;- (void)thisTestMethodTwo;@endm文件
#import "TestClass.h"@interface TestClass ()<TestDelegateTwo>@end@implementation TestClass{    NSString *instanceOne;    TestClassTwo *testTwo;}- (instancetype)init{    self = [super init];    if (self) {        testTwo.delegateTwo = self;    }    return self;}- (void)thisTestMethod{    if ([self.delegate respondsToSelector:@selector(testDelegateMethod)]) {        [self.delegate testDelegateMethod];    }    [testTwo thisTestMethodTwo];}- (void)testDelegateMethodTwo{    NSLog(@"thisTestMethodTwo");}@end@implementation TestClassTwo- (void)thisTestMethodTwo{    if ([self.delegateTwo respondsToSelector:@selector(testDelegateMethodTwo)]) {        [self.delegateTwo testDelegateMethodTwo];    }}@end 3、然后在viewController新建此類,進(jìn)行測試,代碼如下

#import "ViewController.h"#import "TestClass.h"#import <objc/runtime.h>@interface ViewController ()<TestDelegate>@property (nonatomic,strong) TestClass *testClass;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _testClass = [TestClass new];    _testClass.delegate = self;    unsigned int count;    //獲取屬性列表    objc_property_t *propertyList = class_copyPropertyList([_testClass class], &count);    for (int i=0; i<count; i++) {        const char *propertyName = property_getName(propertyList[i]);        NSLog(@"property---->%@", [NSString stringWithUTF8String:propertyName]);    }        //獲取方法列表    Method *methodList = class_copyMethodList([_testClass class], &count);    for (int i=0; i<count; i++) {        Method method = methodList[i];        NSLog(@"method---->%@", NSStringFromSelector(method_getName(method)));    }        //獲取成員變量列表    Ivar *ivarList = class_copyIvarList([_testClass class], &count);    for (int i=0; i<count; i++) {        Ivar myIvar = ivarList[i];        const char *ivarName = ivar_getName(myIvar);        NSLog(@"Ivar---->%@", [NSString stringWithUTF8String:ivarName]);    }    [_testClass thisTestMethod];    //獲取協(xié)議列表    __unsafe_unretained Protocol **protocolList = class_copyProtocolList([_testClass class], &count);    for (int i=0; i<count; i++) {        Protocol *myProtocal = protocolList[i];        const char *protocolName = protocol_getName(myProtocal);        NSLog(@"protocol---->%@", [NSString stringWithUTF8String:protocolName]);    }}- (void)testDelegateMethod{    NSLog(@"12111");}控制臺輸出如下

2017-02-06 18:34:39.920 RuntimeDemo[10997:466991] property---->innerPrOne2017-02-06 18:34:39.921 RuntimeDemo[10997:466991] property---->delegate2017-02-06 18:34:39.921 RuntimeDemo[10997:466991] property---->outPrOne2017-02-06 18:34:39.922 RuntimeDemo[10997:466991] property---->hash2017-02-06 18:34:39.922 RuntimeDemo[10997:466991] property---->superclass2017-02-06 18:34:39.923 RuntimeDemo[10997:466991] property---->description2017-02-06 18:34:39.923 RuntimeDemo[10997:466991] property---->debugDescription2017-02-06 18:34:39.924 RuntimeDemo[10997:466991] method---->testDelegateMethodTwo2017-02-06 18:34:39.925 RuntimeDemo[10997:466991] method---->thisTestMethod2017-02-06 18:34:39.926 RuntimeDemo[10997:466991] method---->outPrOne2017-02-06 18:34:39.926 RuntimeDemo[10997:466991] method---->setOutPrOne:2017-02-06 18:34:39.927 RuntimeDemo[10997:466991] method---->innerPrOne2017-02-06 18:34:39.927 RuntimeDemo[10997:466991] method---->setInnerPrOne:2017-02-06 18:34:39.928 RuntimeDemo[10997:466991] method---->.cxx_destruct2017-02-06 18:34:39.928 RuntimeDemo[10997:466991] method---->setDelegate:2017-02-06 18:34:39.929 RuntimeDemo[10997:466991] method---->delegate2017-02-06 18:34:39.929 RuntimeDemo[10997:466991] method---->init2017-02-06 18:34:39.930 RuntimeDemo[10997:466991] Ivar---->instanceOne2017-02-06 18:34:39.931 RuntimeDemo[10997:466991] Ivar---->testTwo2017-02-06 18:34:39.931 RuntimeDemo[10997:466991] Ivar---->_delegate2017-02-06 18:34:39.932 RuntimeDemo[10997:466991] Ivar---->_outPrOne2017-02-06 18:34:39.932 RuntimeDemo[10997:466991] Ivar---->_innerPrOne2017-02-06 18:34:39.933 RuntimeDemo[10997:466991] 121112017-02-06 18:34:39.933 RuntimeDemo[10997:466991] protocol---->TestDelegateTwo由此可知此類所有屬性與方法,以及該類所遵守的協(xié)議在下一篇文章我們將介紹如何運(yùn)用Runtime進(jìn)行方法攔截


上一篇:ADB命令的使用

下一篇:integer綜合分析

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑水县| 芷江| 新民市| 吉木乃县| 广安市| 正镶白旗| 许昌市| 四平市| 曲阜市| 东平县| 抚远县| 泰安市| 青浦区| 故城县| 公主岭市| 博罗县| 仁寿县| 伊金霍洛旗| 马鞍山市| 武宣县| 深水埗区| 烟台市| 霞浦县| 承德市| 类乌齐县| 灵寿县| 涞水县| 鹰潭市| 祁连县| 弥勒县| 西昌市| 城口县| 瓮安县| 阿鲁科尔沁旗| 肇源县| 五台县| 监利县| 什邡市| 鄂伦春自治旗| 宁强县| 淳安县|