Objective-C的運(yùn)行時(shí)常用方法

前言

runtime是OC成為面向?qū)ο蟮幕A(chǔ),了解runtime的基礎(chǔ)用法對(duì)理解OC非常必要。大量的開(kāi)源庫(kù),如MJExtension,就是使用運(yùn)行時(shí)動(dòng)態(tài)獲取類(lèi)的屬性并實(shí)現(xiàn)動(dòng)態(tài)賦值。熟悉runtime的方法也是看開(kāi)源庫(kù)的基礎(chǔ)之一。

一、OC方法篇

OC對(duì)象的方法調(diào)用是利用selector尋找對(duì)應(yīng)的imp指針進(jìn)行調(diào)用。
現(xiàn)在我們?cè)诖a中理解原理。
定義三個(gè)函數(shù),該方法名為imp指針

void testFunc1(id self, SEL _cmd) {

NSLog(@"實(shí)現(xiàn)testFunc1");

}
void testFunc2(id self, SEL _cmd) {
    
    NSLog(@"實(shí)現(xiàn)testFunc2");
    
}
void testFunc3(id self, SEL _cmd) {
    
    NSLog(@"實(shí)現(xiàn)testFunc3");
    
}

添加實(shí)例方法

//  添加imp到sel,該方法返回一個(gè)bool值,成功為YES
    BOOL isAddInstanceMethodFormImpSuccess = class_addMethod(class, sel_registerName("instanceMethod"), (IMP)testFunc1, "v@:");
    if (isAddInstanceMethodFormImpSuccess) {
        NSLog(@"添加imp方法成功");
    }

添加類(lèi)方法

    BOOL isAddClassMethodFormImpSuccess = class_addMethod(object_getClass(class), sel_registerName("classMethod"), (IMP)testFunc3, "v@:");
    if (isAddClassMethodFormImpSuccess) {
        NSLog(@"添加imp方法成功");
    }

取得blcok的imp指針

  IMP impOfBlock = imp_implementationWithBlock(^(id obj, NSString *string,...){
      NSLog(@"調(diào)用者:%@\nstring:%@", obj, string);
  });
  ```
  添加impOfBlock到sel
  BOOL isAddMethodFormImpOfBlockSuccess = class_addMethod(class, sel_registerName("instanceMethodImpOfBlock:"), impOfBlock, "v@:@");
  if (isAddMethodFormImpOfBlockSuccess) {
      NSLog(@"添加impOfBlock方法成功");
  }
  ```

添加方法后,由于這是運(yùn)行時(shí)添加的,我們不能像平時(shí)那樣調(diào)用,要用使用performSelector系列方法進(jìn)行調(diào)用

[object performSelector:@selector(instanceMethod)];
[object performSelector:@selector(instanceMethodImpOfBlock:) withObject:@"我要調(diào)用Block"];
[(id)class performSelector:@selector(classMethod)];

使用UIView作為測(cè)試類(lèi),該viewController的view作為測(cè)試對(duì)象,該結(jié)果為

2016-08-29 00:20:52.404 RuntimeDemo[3515:1637694] 實(shí)現(xiàn)testFunc1
2016-08-29 00:20:52.404 RuntimeDemo[3515:1637694] 調(diào)用者:<UIView: 0x7ffbdbb04f80; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7ffbdbb03580>>
string:我要調(diào)用Block
2016-08-29 00:20:52.405 RuntimeDemo[3515:1637694] 實(shí)現(xiàn)testFunc3


##二,成員變量與屬性篇
由于在OC在只有在類(lèi)的創(chuàng)建期間才能添加ivar,但是我們啟動(dòng)app時(shí),類(lèi)都創(chuàng)建好了,無(wú)法添加,所以我在在運(yùn)行期間自己創(chuàng)建一個(gè)類(lèi)

根據(jù)名字取得類(lèi)
Class testClass = objc_getClass("MLView");
if (!testClass) {
//動(dòng)態(tài)創(chuàng)建類(lèi)
testClass = objc_allocateClassPair([UIView class], "MLView", 0);
}

添加ivar

BOOL isAddIvarSuccess = class_addIvar(class, "_runtimeString", sizeof(NSString *), log(sizeof(NSString *)), "@"NSString"");
if (isAddIvarSuccess) {
NSLog(@"添加ivar成功");
}

添加property
objc_property_attribute_t type = { "T", "@\"NSString\"" };
 objc_property_attribute_t backingivar  = { "V", "_runtimeString" };
objc_property_attribute_t attrs[] = {type, backingivar};
BOOL isAddPropertySuccess = class_addProperty(class, "runtimeString", attrs, sizeof(attrs)/sizeof(objc_property_attribute_t));
if (isAddPropertySuccess) {
    NSLog(@"添加property成功");
}
 注冊(cè)類(lèi)名,并將viewController的view的class設(shè)置為運(yùn)態(tài)創(chuàng)建的類(lèi)(實(shí)踐上不建議這樣做,不易檢查錯(cuò)誤,如要添加屬性可以變相用關(guān)聯(lián)添加,下文用介紹如何在分類(lèi)添加屬性)

objc_registerClassPair(testClass);
//使用view作為 測(cè)試對(duì)象
id testObject =self.view;
//設(shè)置testObject的類(lèi),就可以使用動(dòng)態(tài)創(chuàng)建的ivar
object_setClass(testObject, testClass);

添加完變量,成功了一半,現(xiàn)在開(kāi)始測(cè)試是否添加成功就要進(jìn)行賦值操作

  獲取ivar并賦值
Ivar ivar = class_getInstanceVariable(class, "_runtimeString");
object_setIvar(object, ivar, @"addIvar測(cè)試字符串");
NSLog(@"addIvar的值:%@", object_getIvar(object, ivar));
  獲取property并賦值
objc_property_t property = class_getProperty(class, "runtimeString");
const char *propertyAttr = property_getAttributes(property);
NSLog(@"addProperty的細(xì)節(jié):%s", propertyAttr);
[object setValue:@"addProperty測(cè)試字符串" forKey:@"_runtimeString"];
NSLog(@"addProperty的值:%@", [object valueForKey:@"_runtimeString"]);
運(yùn)行后得到的結(jié)果為

2016-08-29 00:34:15.870 RuntimeDemo[8155:1663698] addIvar的值:addIvar測(cè)試字符串
2016-08-29 00:34:15.870 RuntimeDemo[8155:1663698] addProperty的細(xì)節(jié):T@"NSString",V_runtimeString
2016-08-29 00:34:18.211 RuntimeDemo[8155:1663698] addProperty的值:addProperty測(cè)試字符串



##三、運(yùn)行時(shí)簡(jiǎn)單封裝
####1、在分類(lèi)中添加屬性
現(xiàn)在我要在給NSObject分類(lèi)里添加一個(gè)對(duì)象

@property (nonatomic, strong) NSString *featureIdentifier;

然后重寫(xiě)setter和getter方法,并用關(guān)聯(lián)機(jī)制
  • (void)setFeatureIdentifier:(NSString *)featureIdentifier
    {
    objc_setAssociatedObject(self, @selector(featureIdentifier), featureIdentifier, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
  • (NSString *)featureIdentifier
    {
    return objc_getAssociatedObject(self, @selector(featureIdentifier));
    }
####2、獲取該類(lèi)的ivar列表
  • (NSArray *)arrayOfIvars
    {
    unsigned int count = 0;
    Ivar *ivar = class_copyIvarList(self, &count);
    NSMutableArray *ivarNameArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<count; i++) {
    Ivar iva = ivar[i];
    const char *name = ivar_getName(iva);
    NSString *strName = [NSString stringWithUTF8String:name];
    [ivarNameArray addObject:strName];

    }

    free(ivar);
    return ivarNameArray;

}

####3、取得該類(lèi)屬性列表
  • (NSArray *)arrayOfProperties
    {
    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList(self, &count);

    NSMutableArray *propertyNameArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<count; i++) {
    objc_property_t property = properties[i];
    const char *name = property_getName(property);
    NSString *strName = [NSString stringWithUTF8String:name];
    [propertyNameArray addObject:strName];

    }

    free(properties);
    return propertyNameArray;

}


####4、取得該類(lèi)實(shí)例方法列表
  • (NSArray *)arrayOfInstanceMethods
    {
    unsigned int count = 0;
    Method *methodList = class_copyMethodList(self, &count);
    NSMutableArray *methods = [[NSMutableArray alloc] init];

    for (NSInteger i = 0; i < count; i++) {
    SEL selector = method_getName(methodList[i]);
    NSString *selString = NSStringFromSelector(selector);
    [methods addObject:selString];
    }

    free(methodList);
    return methods;
    }

####5、取得該類(lèi)方法方法列表
unsigned int count = 0;
Method *methodList = class_copyMethodList(object_getClass(self), &count);
NSMutableArray *methods = [[NSMutableArray alloc] init];

for (NSInteger i = 0; i < count; i++) {
    SEL selector = method_getName(methodList[i]);
    NSString *methodString = NSStringFromSelector(selector);
    [methods addObject:methodString];
}

free(methodList);
return methods;
####6、取得該類(lèi)遵循協(xié)議列表
  • (NSArray *)arrayOfProtocols{

    unsigned int count = 0;
    Protocol * __unsafe_unretained *protocols = class_copyProtocolList(self, &count);
    NSMutableArray *protocolArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
    [protocolArray addObject:[NSString stringWithUTF8String:protocol_getName(protocols[i])]];

    }

free(protocols);
return protocolArray;

}

####7、取得該工程所有類(lèi)的列表
  • (NSArray *)arrayOfAllClass {
    NSMutableArray * classList = [NSMutableArray array];

    unsigned int classesCount = 0;
    Class * classes = objc_copyClassList(&classesCount);

for ( int i = 0; i < classesCount; ++i) {
    NSString *className = NSStringFromClass(classes[i]);

    [classList addObject:className];
}

free(classes);
return classList;

}

####8、取得該對(duì)象中有值的的property字典
  • (NSDictionary *)dictionaryOfPropertyKeyValues
    {
    NSArray *properties = [[self class] arrayOfProperties];
    NSMutableDictionary *keyValueDictionary = [[NSMutableDictionary alloc] init];
    for (NSInteger i = 0; i < properties.count; i++) {
    if ([self valueForKey:properties[i]]) {
    [keyValueDictionary setObject:[self valueForKey:properties[i]] forKey:properties[i]];
    }
    }
    return keyValueDictionary;
    }

[此處應(yīng)該有demo][https://github.com/luomagaoshou/MLRuntimeDemo]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,058評(píng)論 0 9
  • 我們常常會(huì)聽(tīng)說(shuō) Objective-C 是一門(mén)動(dòng)態(tài)語(yǔ)言,那么這個(gè)「動(dòng)態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,338評(píng)論 0 7
  • Runtime是一套比較底層的純C語(yǔ)言API,包含了很多底層的C語(yǔ)言API。在我們平時(shí)編寫(xiě)的OC代碼中,程序運(yùn)行時(shí)...
    這個(gè)年紀(jì)的情愫丶閱讀 693評(píng)論 5 3
  • 聽(tīng)力和單詞是硬傷 sad 擦干眼淚 還得繼續(xù)前進(jìn)
    不ting下腳步閱讀 222評(píng)論 0 0
  • 磁性藍(lán)鷹kin235 圖表是由App所計(jì)算出來(lái)的星系印記。我們也可通過(guò)計(jì)算找出自己的Kin,并且在卓爾金歷找到自己...
    ChrisLooi閱讀 344評(píng)論 0 0

友情鏈接更多精彩內(nèi)容