美文网首页
runtime-API-protocol

runtime-API-protocol

作者: Berning | 来源:发表于2023-11-13 18:02 被阅读0次

    1.获取协议

        Protocol *protocol = objc_getProtocol("NSPersonDelegate");
        NSLog(@"protocol:%@",NSStringFromProtocol(protocol));
        
    

    2.拷贝协议列表

        unsigned int outCount;
        Protocol * __unsafe_unretained * protocols = objc_copyProtocolList(&outCount);
        for (int i = 0; i < outCount; i++) {
            
    //        NSLog(@"protocols_%d:%s",i,protocol_getName(protocols[i]));
        }
    

    3.判断协议是否遵守了另一个协议

        BOOL is_conform = protocol_conformsToProtocol(@protocol(NSPersonDelegate), @protocol(NSObject));
        NSLog(@"is_conform:%d",is_conform);
        
    

    4.判断两个协议是否为同一个协议

       //获取Protocol
       //objc_getProtocol(const char * _Nonnull name)
       //@protocol
       //NSProtocolFromString(NSString * _Nonnull namestr)
       BOOL is_equal = protocol_isEqual(objc_getProtocol("NSPersonDelegate"), @protocol(NSPersonDelegate));
       
       NSLog(@"is_equal:%d",is_equal);
    

    5.获取协议名

        NSLog(@"protocol_getName:%s",protocol_getName(@protocol(NSPersonDelegate)));
    

    6.获取方法描述

        struct objc_method_description dance_description =  protocol_getMethodDescription(@protocol(NSPersonDelegate), @selector(dance), 1, 1);
        
        struct objc_method_description sing_description =  protocol_getMethodDescription(@protocol(NSPersonDelegate), @selector(sing), 1, 0);
    
        struct objc_method_description work_description =  protocol_getMethodDescription(@protocol(NSPersonDelegate), @selector(work), 0, 1);
    
        struct objc_method_description rest_description =  protocol_getMethodDescription(@protocol(NSPersonDelegate), @selector(rest), 0, 0);
    
        
        NSLog(@"dance_description:%@,%s",NSStringFromSelector(dance_description.name),dance_description.types);
        
        NSLog(@"sing_description:%@,%s",NSStringFromSelector(sing_description.name),sing_description.types);
        
        NSLog(@"work_description:%@,%s",NSStringFromSelector(work_description.name),work_description.types);
        
        NSLog(@"rest_description:%@,%s",NSStringFromSelector(rest_description.name),rest_description.types);
    
    

    7.获取方法描述列表

       
        unsigned int protocol_count;
        struct objc_method_description *protocol_descriptions = protocol_copyMethodDescriptionList(@protocol(NSPersonDelegate), 1, 1, &protocol_count);
        
        for (int i = 0; i < protocol_count; i++) {
            struct objc_method_description protocol_desc = *(protocol_descriptions + i);
            NSLog(@"protocol_desc_%i:%@,%s",i,NSStringFromSelector(protocol_desc.name),protocol_desc.types);
        }
    

    8.获取指定属性

        objc_property_t grade_property = protocol_getProperty(@protocol(NSPersonDelegate), "grade", 1, 1);
        NSLog(@"grade_property:%s",property_getName(grade_property));
    

    9.拷贝属性列表

        //Returns an array of the required instance properties declared by a protocol
        unsigned int property_count_0;
        objc_property_t *properties_0 = protocol_copyPropertyList(@protocol(NSPersonDelegate), &property_count_0);
        
        for(int i = 0; i < property_count_0; i++)
        {
            NSLog(@"properties_0_%i:%s",i,property_getName(*(properties_0 + i)));
        }
        free(properties_0);
    

    10.获取属性列表

        //10. Returns an array of properties declared by a protocol.
        
        //isRequiredProperty 必须是YES,获取到require和optional ,NO获取到NULL
        unsigned int property_count_1;
    
        objc_property_t *properties_1 = protocol_copyPropertyList2(@protocol(NSPersonDelegate), &property_count_1, 1, 0);
        
        for(int i = 0; i < property_count_1; i++)
        {
            NSLog(@"properties_1_%i:%s",i,property_getName(*(properties_1 + i)));
        }
        
        free(properties_1);
    

    11.拷贝所遵守的协议列表

        unsigned int protocol_protocol_count;
        Protocol * __unsafe_unretained *protocol_protocols = protocol_copyProtocolList(@protocol(NSPersonDelegate), &protocol_protocol_count);
        for (int i = 0 ; i < protocol_protocol_count; i++) {
            NSLog(@"protocol_protocols_%i:%s",i,protocol_getName(*(protocol_protocols + i)));
        }
        
        free(protocol_protocols);
    

    12.动态创建协议

        Protocol *NSPersonDataSource = objc_allocateProtocol("NSPersonDataSource");
        
        //12-1.
        protocol_addMethodDescription(NSPersonDataSource, @selector(abc), "v@:", 1, 1);
        struct objc_method_description abc_description =  protocol_getMethodDescription(NSPersonDataSource, @selector(abc), 1, 1);
        NSLog(@"abc_description:%@,%s",NSStringFromSelector(abc_description.name),abc_description.types);
    
    
        
        //12-2.
        protocol_addProtocol(NSPersonDataSource, @protocol(NSCopying));
        unsigned int protocol_protocol_count2;
        Protocol * __unsafe_unretained *protocol_protocols_2 = protocol_copyProtocolList(NSPersonDataSource, &protocol_protocol_count2);
        for (int i = 0 ; i < protocol_protocol_count; i++) {
            NSLog(@"protocol_protocols_%i:%s",i,protocol_getName(*(protocol_protocols_2 + i)));
        }
        
        free(protocol_protocols_2);
    
        
        //12-3.
        
        objc_property_attribute_t name_t = {"T","@\"NSString\""};
        objc_property_attribute_t name_owner = {"C",""};
        objc_property_attribute_t name_atomic = {"N",""};
        objc_property_attribute_t name_rw = {"R",""};
        objc_property_attribute_t name_i = {"V","_name"};
        objc_property_attribute_t name_attr[] = {name_t,name_owner,name_atomic,name_rw,name_i};
        
        protocol_addProperty(NSPersonDataSource, "name", name_attr, 5, 1, 1);
        
        
        objc_property_t name_property = protocol_getProperty(NSPersonDataSource, "name", 1, 1);
        NSLog(@"grade_property:%s,%s",property_getName(name_property),property_getAttributes(name_property));
        
    
        objc_registerProtocol(NSPersonDataSource);
    
    

    相关文章

      网友评论

          本文标题:runtime-API-protocol

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