Runtime方法的使用—Protocol、SEL篇

本文详细探讨了Objective-C Runtime中Protocol和SEL的使用,通过实例讲解如何在运行时动态调用方法,揭示了Objective-C的动态特性。
摘要由CSDN通过智能技术生成

本篇主要是Protocol、SEL相关的方法调用


完整代码在这里

#pragma mark - IBAction
- (IBAction)logRunTimeAction:(id)sender {
    objc_property_attribute_t attrs[] = { { "T", "@\"NSString\"" }, { "&", "N" }, { "V", "" } };

    size_t objSize = class_getInstanceSize([_person class]);
    size_t allocSize = 2 * objSize;
    uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);

    // Sel
    [self sel_getName:@selector(sel_getName:)];
    [self sel_registerName:"runtimeTestAction3"];
    [self sel_getUid:"runtimeTestAction3"];
    [self sel_isEqual:@selector(runtimeTestAction3) sel2:@selector(runtimeTestAction2)];

    // Protocol
    [self protocol_getName:protocol];
    [self protocol_getProperty:NSProtocolFromString(@"RuntimeBaseProtocol") name:"protocolString" isRequiredProperty:YES isInstanceProperty:YES];

    Protocol *testProtocol = [self objc_allocateProtocol:"TestProtocol"];
    [self protocol_addProperty:testProtocol name:"newProperty" attributes:attrs attributeCount:3 isRequiredProperty:YES isInstanceProperty:YES];
    [self protocol_addProtocol:testProtocol addition:protocol];
    [self protocol_addMethodDescription:testProtocol sel:@selector(runtimeTestAction3) types:"v@:" isRequiredMethod:NO isInstanceMethod:YES]; // 已注册的协议无法添加

    [self objc_registerProtocol:testProtocol];

    [self protocol_copyPropertyList:testProtocol];
    [self protocol_copyProtocolList:testProtocol];
    [self protocol_copyMethodDescriptionList:testProtocol isRequiredMethod:NO isInstanceMethod:YES];
    [self protocol_isEqual:testProtocol other:protocol];
    [self protocol_conformsToProtocol:testProt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值