OC-runtime使用场景

在实际开发中,Objective-C 的运行时(runtime)是一个强大的特性,它允许开发者在程序运行时动态地操作类、对象、方法等,以下是一些常见的使用场景:

  1. 关联对象

    有时需要为类添加额外的属性,但又不想修改类的声明。通过 runtime 可以为类的实例动态地关联额外的数据。例如,在一个第三方库提供的类中,我们想为它添加一个自定义的标识属性,就可以使用关联对象来实现。

    objc

    less 复制代码
    #import <objc/runtime.h>
    
    @interface UIButton (CustomProperty)
    @property (nonatomic, copy) NSString *customIdentifier;
    @end
    
    @implementation UIButton (CustomProperty)
    - (void)setCustomIdentifier:(NSString *)customIdentifier {
        objc_setAssociatedObject(self, @selector(customIdentifier), customIdentifier, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (NSString *)customIdentifier {
        return objc_getAssociatedObject(self, @selector(customIdentifier));
    }
    @end
  2. 方法交换

    这是一种非常有用的技术,用于在不修改原有方法实现的基础上,对方法的行为进行修改或扩展。比如在应用中,我们想统计某个方法的调用次数或在方法调用前后添加一些日志记录。

    objc

    less 复制代码
    Method originalMethod = class_getInstanceMethod([UIViewController class], @selector(viewDidLoad));
    Method swappedMethod = class_getInstanceMethod([UIViewController class], @selector(myViewDidLoad));
    method_exchangeImplementations(originalMethod, swappedMethod);
    
    - (void)myViewDidLoad {
        NSLog(@"ViewDidLoad method start");
        [self myViewDidLoad];
        NSLog(@"ViewDidLoad method end");
    }
  3. 动态创建类和对象

    在运行时可以动态地创建类,然后实例化对象。这在一些框架或插件系统中很有用,比如根据配置文件中的类名动态地创建类实例。

    objc

    less 复制代码
    Class newClass = objc_allocateClassPair([NSObject class], "MyDynamicClass", 0);
    // 添加属性和方法
    class_addProperty(newClass, "dynamicProperty", propertyAttributes);
    class_addMethod(newClass, @selector(dynamicMethod), (IMP)dynamicMethodImplementation, methodTypes);
    objc_registerClassPair(newClass);
    
    id instance = [[newClass alloc] init];
  4. 获取类的信息

    可以使用 runtime 获取类的属性列表、方法列表、协议列表等信息。这在实现一些自动序列化、反射机制或代码生成工具时非常有用。

    objc

    ini 复制代码
    unsigned int propertyCount;
    objc_property_t *properties = class_copyPropertyList([MyClass class], &propertyCount);
    for (unsigned int i = 0; i < propertyCount; i++) {
        const char *propertyName = property_getName(properties[i]);
        NSLog(@"Property: %s", propertyName);
    }
    free(properties);
  5. 消息转发机制

    当一个对象接收到无法响应的消息时,runtime 会启动消息转发流程。通过重写相关的消息转发方法,可以实现更灵活的消息处理。比如在一个代理模式中,当代理对象无法处理某个消息时,可以将消息转发给其他对象处理。

    objc

    python 复制代码
    - (id)forwardingTargetForSelector:(SEL)aSelector {
        if ([self.anotherObject respondsToSelector:aSelector]) {
            return self.anotherObject;
        }
        return [super forwardingTargetForSelector:aSelector];
    }

这些只是 runtime 在实际开发中的一部分应用,它为开发者提供了高度的灵活性和扩展性,能够实现很多强大的功能。

相关推荐
我叫张小白。9 分钟前
Vue3 Hooks:逻辑复用的解决方案
前端·javascript·vue.js·前端框架·vue
S***t71412 分钟前
前端物联网开发
前端·物联网
我叫张小白。20 分钟前
Vue3 Props 的使用:组件间数据传递的桥梁
前端·javascript·vue.js·vue3
r***869820 分钟前
Nginx解决前端跨域问题
运维·前端·nginx
广州华水科技27 分钟前
单北斗GNSS在桥梁变形监测中的关键应用与技术优势分析
前端
IT_陈寒28 分钟前
Python 3.12新特性实战:10个让你效率翻倍的代码优化技巧
前端·人工智能·后端
z***948429 分钟前
Redis 6.2.7安装配置
前端·数据库·redis
2301_8072886330 分钟前
MPRPC项目制作(第四天)
java·服务器·前端
J***793931 分钟前
前端在移动端中的React Native Windows
前端·react native·react.js
阿雄不会写代码32 分钟前
PPTX报错AttributeError: module ‘collections‘ has no attribute ‘Container‘
前端