美文网首页
Runtime初见

Runtime初见

作者: 御雪飞斐 | 来源:发表于2017-03-12 21:41 被阅读13次

Objective-C Runtime是一个将C语言转化为面向对象语言的扩展。
我们将C++和Objective进行对比,虽然C++和Objective-C都是在C的基础上加入面向对象的特性扩充而成的程序设计语言,但二者实现的机制差异很大。C++是基于静态类型,而Objective-C是基于动态运行时类型。
就是说用C++编写的程序通过编译器直接把函数地址硬编码进入可执行文件;而Objective-C无法通过编译器直接把函数地址硬编码进入可执行文件,而是在程序运行的时候,利用Runtime根据条件判断作出决定。函数标识与函数过程的真正内容之间的关联可以动态修改。Runtime是Objective不可缺少的重要一部分。

类与对象的区别就是类比对象多了很多特征成员,�类也可以当做一个objc_object来对待,也就是说类和对象都是对象,分别称作类对象(class object)和实例对象(instance object),这样我们就可以区别对象和类了。

runtime机制为我们提供了一系列的方法让我们可以在程序运行时动态修改类、对象中的所有属性、方法。

下面就介绍运行时一种很常见的使用方式,字典转模型。当然,你可能会说,“我用KVO直接 setValuesForKeysWithDictionary: 传入一个字典一样可以快速将字典转模型啊”,但是这种方法有它的弊端,只有遍历某个模型中所有的成员变量,然后通过成员变量从字典中取出对应的值并赋值最为稳妥,由于篇幅有限,这里暂且不讨论那么多,你权且当作多认识一种数据转模型的方式,以及初步认识一下runtime的强大。

@interface Lender : NSObject{
    CGFloat height;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, assign) int no;
@end

在其它文件使用这个类,注意:要使用运行时,必须先包含

import <objc/message.h>

unsigned int outCount = 0;
    Ivar *vars = class_copyIvarList([Lender class], &outCount); // 获取到所有的变量列表
    
    // 遍历所有的成员变量
    for (int i = 0; i < outCount; i++) {
        Ivar ivar = vars[i]; // 取出第i个位置的成员变量
        
        const char *propertyName = ivar_getName(ivar); // 通过变量获取变量名
        const char *propertyType = ivar_getTypeEncoding(ivar); // 获取变量编码类型
        printf("---%s--%s\n", propertyName, propertyType);

    }

runtime实现JSON和Model互转:

#import <Foundation/Foundation.h>

@interface People : NSObject

@property (nonatomic, copy) NSString *name; // 姓名
@property (nonatomic, strong) NSNumber *age; // 年龄
@property (nonatomic, copy) NSString *occupation; // 职业
@property (nonatomic, copy) NSString *nationality; // 国籍

// 生成model
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

// 转换成字典
- (NSDictionary *)covertToDictionary;

@end
#import "People.h"

#if TARGET_IPHONE_SIMULATOR
#import <objc/objc-runtime.h>
#else
#import <objc/runtime.h>
#import <objc/message.h>
#endif

@implementation People

- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];

    if (self) {
        for (NSString *key in dictionary.allKeys) {
            id value = dictionary[key];

            SEL setter = [self propertySetterByKey:key];
            if (setter) {
                // 这里还可以使用NSInvocation或者method_invoke,不再继续深究了,有兴趣google。
                ((void (*)(id, SEL, id))objc_msgSend)(self, setter, value);
            }
        }
    }
    return self;
}

- (NSDictionary *)covertToDictionary
{
    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    if (count != 0) {
        NSMutableDictionary *resultDict = [@{} mutableCopy];

        for (NSUInteger i = 0; i < count; i ++) {
            const void *propertyName = property_getName(properties[i]);
            NSString *name = [NSString stringWithUTF8String:propertyName];

            SEL getter = [self propertyGetterByKey:name];
            if (getter) {
                id value = ((id (*)(id, SEL))objc_msgSend)(self, getter);
                if (value) {
                    resultDict[name] = value;
                } else {
                    resultDict[name] = @"字典的key对应的value不能为nil哦!";
                }

            }
        }

        free(properties);

        return resultDict;
    }

    free(properties);

    return nil;
}

#pragma mark - private methods

// 生成setter方法
- (SEL)propertySetterByKey:(NSString *)key
{
    // 首字母大写,你懂得
    NSString *propertySetterName = [NSString stringWithFormat:@"set%@:", key.capitalizedString];

    SEL setter = NSSelectorFromString(propertySetterName);
    if ([self respondsToSelector:setter]) {
        return setter;
    }
    return nil;
}

// 生成getter方法
- (SEL)propertyGetterByKey:(NSString *)key
{
    SEL getter = NSSelectorFromString(key);
    if ([self respondsToSelector:getter]) {
        return getter;
    }
    return nil;
}

@end

main.m中运行以下代码:

#import <Foundation/Foundation.h>

#if TARGET_IPHONE_SIMULATOR
#import <objc/objc-runtime.h>
#else
#import <objc/runtime.h>
#import <objc/message.h>
#endif

#import "People.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSDictionary *dict = @{
                               @"name" : @"苍井空",
                               @"age"  : @18,
                               @"occupation" : @"老师",
                               @"nationality" : @"日本"
                               };

        // 字典转模型
        People *cangTeacher = [[People alloc] initWithDictionary:dict];
        NSLog(@"热烈欢迎,从%@远道而来的%@岁的%@%@",cangTeacher.nationality,cangTeacher.age,cangTeacher.name,cangTeacher.occupation);

        // 模型转字典
        NSDictionary *covertedDict = [cangTeacher covertToDictionary];
        NSLog(@"%@",covertedDict);

    }
    return 0;
}

相关文章

  • Runtime初见

    Objective-C Runtime是一个将C语言转化为面向对象语言的扩展。我们将C++和Objective进行...

  • Runtime之初见

    人生若只如初见,何事秋风悲画扇。 依旧是网上很多runtime的资料,依旧是看不懂,,,这里给大家转化一下runt...

  • runtime之初见

    1、获取成员变量 Ivar表示成员类型。使用runtime需要导入,使用msgSe...

  • iOS之RunTime探索与实践

    Runtime 概念 Runtime 相关概念 Runtime 实践 Runtime概念 Runtime简称运行时...

  • OC -> Runtime

    Runtime简介 Runtime用处 Runtime实践 Runtime 类方法调用实现。Person * p ...

  • iOS runtime(三)runtime之method(1)m

    iOS runtime(一)runtime之Property 详尽iOS runtime(二)runtime之Iv...

  • java中调用python的几种方式(记录)

    第一种:通过runtime Runtime runtime = Runtime.getRuntime(); try...

  • 自己实现OC的KVO

    Runtime系列文章在这:Runtime介绍---术语介绍Runtime应用--动态添加方法Runtime应用-...

  • 详解runTime和runLoop

    runTime 和 runLoop runTime的详解: 1.什么是runtime? runtime即运行时,它...

  • iOS面试-Runtime简介

    本文主要介绍runtime的五点 Runtime简介 Runtime(消息机制) Runtime方法调用流程 Ru...

网友评论

      本文标题:Runtime初见

      本文链接:https://www.haomeiwen.com/subject/ncuxnttx.html