Runtime方法的使用—Object篇

本篇主要是Object相关的方法调用

完整代码在这里

#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);

    // Object
    [self object_getInstanceVariable:_person name:"_name" outValue:nil];
    [self object_getClassName:_person];
    [self object_getClass:_person];
    [self objc_getClass:"Person"];
    [self objc_getMetaClass:"Person"];
    [self objc_getProtocol:"RuntimeBaseProtocol"];
    [self object_copy:_person size:class_getInstanceSize([_person class])];
// [self objc_copyProtocolList];//打印大量数据
// [self objc_copyClassList];// 打印大量信息
    [self object_setInstanceVariable:_person name:"_name" value:@"newName"];
    [self object_setIvar:_person ivar:object_getInstanceVariable(_person, "_name", nil) value:@"NEWNAME"];
    [self objc_setAssociatedObject:_person key:"_title" value:@"dev" policy:OBJC_ASSOCIATION_RETAIN];
    [self object_dispose:nil];
    [self objc_constructInstance:[_person class] bytes:(void *)ptr];
    [self objc_destructInstance:nil];

    Class class = [self objc_allocateClassPair:[_person class] name:"student" size:0];
    [self objc_registerClassPair:class];
    [self objc_disposeClassPair:class];

    Protocol *protocol = [self objc_allocateProtocol:"NewProtocol"];
    [self objc_registerProtocol:protocol];
    [self class_addProtocol:[_person class] protocol:NSProtocolFromString(@"NewProtocol")];
    [self class_copyProtocolList:[_person class]];

}


#pragma mark - Class 创建
- (void)class_createInstance:(Class)class extraBytes:(size_t)extraBytes {
    Person *tempPerson = class_createInstance(class, extraBytes);
    tempPerson.name = @"instance creat Success";
    NSLog(@"%s%@",__func__,tempPerson.name);
}

#pragma mark - Class 类名,父类,元类;实例变量,成员变量;属性;实例方法,类方法,方法实现;
/** * 获取类的类名 * *  @param class 类 */
- (void)class_getClassName:(Class)class {
    NSLog(@"%s:%s",__func__,class_getName(class));
}

/** * 获取类的父类 * *  @param class 类 */
- (void)class_getSuperClass:(Class)class {
    NSLog(@"%s%@",__func__,NSStringFromClass(class_getSuperclass(class)));
}

/** * 获取实例大小 * *  @param class 类 */
- (void)class_getInstanceSize:(Class)class {
    NSLog(@"%s%zu",__func__,class_getInstanceSize(class));
}

/** * 获取类中指定名称实例成员变量的信息 * *  @param class 类 *  @param name 成员变量名 */
- (void)class_getInstanceVariable:(Class)class name:(const char *)name {
    Ivar ivar = class_getInstanceVariable(class,name);
    NSLog(@"%s%s%s",__func__,[self ivar_getTypeEncoding:ivar],[self ivar_getName:ivar]);
}

/** * 获取类成员变量的信息(该函数没有作用,官方解释:http://lists.apple.com/archives/objc-language/2008/Feb/msg00021.html * *  @param class 类 *  @param name 成员变量名 */
- (void)class_getClassVariable:(Class)class name:(const char *)name {
    Ivar ivar = class_getClassVariable(class,name);
    NSLog(@"%s%s%s",__func__,[self ivar_getTypeEncoding:ivar],[self ivar_getName:ivar]);
}

/** * 获取属性的信息(与获取成员变量信息类似,不同的是不用打_) * *  @param class 类 *  @param name 属性名 */
- (void)class_getProperty:(Class)class name:(const char *)name {
    objc_property_t property = class_getProperty(class,name);
    NSLog(@"%s%s%s",__func__,[self property_getName:property] ,[self property_getAttributes:property]);
    [self property_copyAttributeList:property];
}

/** * 获取类制定方法的信息 * *  @param class 类 *  @param selector 方法 */
- (Method)class_getInstanceMethod:(Class)class selector:(SEL)selector {
    Method method = class_getInstanceMethod(class, selector);
    NSLog(@"%s%s%u",__func__,sel_getName(method_getName(method)) ,[self method_getNumberOfArguments:method]);
    return method;
}

/** * 获取类方法的信息 * *  @param class 类 *  @param selector 方法 */
+ (Method)class_getClassMethod:(Class)class selector:(SEL)selector {
    Method method = class_getClassMethod(class, selector);
    NSLog(@"%s%s%u",__func__,sel_getName(method_getName(method)) ,method_getNumberOfArguments(method));
    return method;
}

/** * 获取方法具体实现 * *  @param class 类 *  @param selector 方法 * *  @return IMP */
- (IMP)class_getMethodImplementation:(Class)class selector:(SEL)selector {
    IMP imp = class_getMethodImplementation(class, selector);
    return imp;
}

/** * 获取类中的方法的实现,该方法的返回值类型为struct * *  @param class 类 *  @param selector 方法 * *  @return IMP */
- (IMP)class_getMethodImplementation_stret:(Class)class selector:(SEL)selector {
    IMP imp = class_getMethodImplementation_stret(class, selector);
    return imp;
}

#pragma mark - Class 成员变量列表;属性列表;方法列表;协议列表;
/** * 获取成员变量列表 * *  @param class 类 */
- (void)class_copyIvarList:(Class)class {
    unsigned int count;
    Ivar *ivarList = class_copyIvarList(class, &count);
    NSLog(@"%s",__func__);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        // 获取成员属性名
        NSString *name = [NSString stringWithUTF8String:[self ivar_getName:ivar]];
        NSString *type = [NSString stringWithUTF8String:[self ivar_getTypeEncoding:ivar]];
// NSString *value = object_getIvar(obj, ivar);
        NSLog(@"%@%@",type,name);
    }
}

- (void)class_copyPropertyList:(Class)class {
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList(class,&count);
    NSLog(@"%s",__func__);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        // 获取成员属性名
        NSString *name = [NSString stringWithUTF8String:[self property_getName:property]];
        NSString *type = [NSString stringWithUTF8String:[self property_getAttributes:property]];
        NSLog(@"%@%@",type,name);
    }
}

/** * 获取方法列表 * *  @param class 类 */
- (void)class_copyMethodList:(Class)class {
    unsigned int count;
    Method *methodList = class_copyMethodList(class,&count);
    for (int i = 0; i < count; i++) {
        Method method = methodList[i];
        NSLog(@"%s%s",__func__,sel_getName(method_getName(method)));
    }
}

/** * 获取协议列表 * *  @param class 类 */
- (void)class_copyProtocolList:(Class)class {
    unsigned int count;
    Protocol **protocolList = class_copyProtocolList(class,&count);
    for (int i = 0; i < count; i++) {
        Protocol *protocol = protocolList[i];
        NSLog(@"%s%s",__func__,[self protocol_getName:protocol]);
    }
}

#pragma mark - Class add: 成员变量;属性;方法;协议
/** * 添加成员变量(添加成员变量只能在运行时创建的类,且不能为元类) * *  @param class 类 *  @param name 成员变量名字 *  @param size 大小 *  @param alignment 对其方式 *  @param types 参数类型 */
- (void)class_addIvar:(Class)class name:(const char *)name size:(size_t)size alignment:(uint8_t)alignment types:(const char *)types {

// if (class_addIvar([_person class], "country", sizeof(NSString *), 0, "@")) {
    if (class_addIvar(class, name, size, alignment, types)) {

        NSLog(@"%sadd ivar success",__func__);
    }else{
        NSLog(@"%sadd ivar fail",__func__);
    }
}

/** * 添加属性 * *  @param class 类 *  @param name 属性名 *  @param attributes 参数 *  @param attributeCount 参数数量 */
- (void)class_addProperty:(Class)class name:(const char *)name attributes:(const objc_property_attribute_t *)attributes attributeCount:(unsigned int)attributeCount {
    objc_property_attribute_t type = { "T", "@\"NSString\"" };
    objc_property_attribute_t ownership = { "&", "N" }; // C = copy
    objc_property_attribute_t backingivar  = { "V", "" };
    objc_property_attribute_t attrs[] = { type, ownership, backingivar };

    if (class_addProperty(class, name, attrs, attributeCount)) {
        NSLog(@"%sadd Property success",__func__);
    }else{
        NSLog(@"%sadd Property fail",__func__);
    }
// [self class_copyPropertyList:class];
}

/** * 添加方法 * *  @param class 类 *  @param selector 方法 *  @param imp 方法实现 *  @param types 类型 */
- (void)class_addMethod:(Class)class selector:(SEL)selector imp:(IMP)imp types:(const char *)types {
    if (class_addMethod(class,selector,class_getMethodImplementation(class, selector),types)) {
        NSLog(@"%sadd method success",__func__);
    }else{
        NSLog(@"%sadd method fail",__func__);
    }
// [self class_copyMethodList:class];

}

/** * 添加协议 * *  @param class 类 *  @param protocol 协议 */
- (void)class_addProtocol:(Class)class protocol:(Protocol *)protocol {
    if (class_addProtocol(class, protocol)) {
        NSLog(@"%sadd protocol success",__func__);
    }else{
        NSLog(@"%sadd protocol fail",__func__);
    }
// [self class_copyProtocolList:class];
}


#pragma marl - Class replace:属性;方法
/** * 替换属性的信息(如果没有原属性会新建一个属性) * *  @param class 类 *  @param name 属性名 *  @param attributes 类型 *  @param attributeCount 类型数量 */
- (void)class_replaceProperty:(Class)class name:(const char *)name attributes:(const objc_property_attribute_t *)attributes attributeCount:(unsigned int)attributeCount {
    objc_property_attribute_t type = { "T", "@\"NSString\"" };
    objc_property_attribute_t ownership = { "C", "" }; // C = copy
    objc_property_attribute_t backingivar  = { "V", "" };
    objc_property_attribute_t attrs[] = { type, ownership, backingivar };

    class_replaceProperty(class, name, attrs, 3);
// [self class_copyPropertyList:class];

}

/** * 替代方法的实现 * *  @param class 类 *  @param selector 被替代的方法 *  @param imp 替代方法 *  @param types 类型 */
- (void)class_replaceMethod:(Class)class selector:(SEL)selector imp:(IMP)imp types:(const char *)types {
    class_replaceMethod(class, selector, imp, types);
    NSLog(@"%s",__func__);
    [_person runtimeTestAction1];
}

#pragma mark - Class 判断
/** * 查看类是否相应指定方法 * *  @param class 类 *  @param selector 方法 */
- (void)class_respondsToSelector:(Class)class selector:(SEL)selector {
    if (class_respondsToSelector(class,selector)) {
        NSLog(@"%s %@ exist",__func__,NSStringFromClass(class));
    }else{
        NSLog(@"%s %@ non-exist",__func__,NSStringFromClass(class));
    }
}

/** * 查看类是否为元类 * *  @param class 类 */
- (void)class_isMetaClass:(Class)class {
    if (class_isMetaClass(class)) {
        NSLog(@"%s %@ isMetaClass",__func__,NSStringFromClass(class));
    }else{
        NSLog(@"%s %@ non-isMetaClass",__func__,NSStringFromClass(class));
    }
}

/** * 查看类是否遵循指定协议 * *  @param class 类 *  @param protocol 协议 */
- (BOOL)class_conformsToProtocol:(Class)class protocol:(Protocol *)protocol {
    if (class_conformsToProtocol(class, protocol)) {
        NSLog(@"%s %@ conformsToProtocol %@",__func__,NSStringFromClass(class),NSStringFromProtocol(protocol));
        return YES;
    }else{
        NSLog(@"%s %@ non-conformsToProtocol %@",__func__,NSStringFromClass(class),NSStringFromProtocol(protocol));
        return NO;
    }
}


#pragma mark - Objc get: 实例变量;成员变量;类名;类;元类;关联对象;协议;协议列表
/** * 获取实例的成员变量 * *  @param obj 对象 *  @param name 成员变量名 *  @param outValue 输出值 */
- (void)object_getInstanceVariable:(id)obj name:(const char*)name outValue:(void **)outValue{
    Ivar ivar = object_getInstanceVariable(obj, name, nil);
    NSLog(@"%s personName %@",__func__,[self object_getIvar:obj ivar:ivar]);
}

/** * 获取成员变量的值 * *  @param obj 对象 *  @param ivar 成员变量 * *  @return 值 */
- (id)object_getIvar:(id)obj ivar:(Ivar)ivar {
    return object_getIvar(obj, ivar);
}

/** * 获取指定对象的类名 * *  @param obj 对象 */
- (void)object_getClassName:(id)obj {
    NSLog(@"%s%s",__func__,object_getClassName(obj));
}

/** * 获取指定对象的类 * *  @param obj 对象 */
- (void)object_getClass:(id)obj {
    NSLog(@"%s%@",__func__,NSStringFromClass(object_getClass(obj)));
}

/** * 拷贝指定对象 * *  @param obj 对象 *  @param size 实例大小 */
- (void)object_copy:(id)obj size:(size_t)size {
    Person *tempObj = object_copy(obj, size);
    tempObj.name = @"tempxietao";
    NSLog(@"%s tempPersonName:%@ personName:%@",__func__,tempObj.name,_person.name);
}

/** * 获取指定类名的类 * *  @param name 类名 */
- (void)objc_getClass:(const char *)name {
    NSLog(@"%s%@",__func__,NSStringFromClass(objc_getClass(name)));
}

/** * 获取指定类的元类 * *  @param name 类名 */
- (void)objc_getMetaClass:(const char *)name {
    NSLog(@"%s%@",__func__,NSStringFromClass(objc_getMetaClass(name)));

}

/** * 获取指定名字的协议 * *  @param name 协议名称 */
- (void)objc_getProtocol:(const char *)name {
    Protocol *protocol = objc_getProtocol("RuntimeBaseProtocol");
    NSLog(@"%s %s",__func__,[self protocol_getName:protocol]);
// [self class_conformsToProtocol:[_person class] protocol:protocol];
}

/** * 获取关联对象的值 * *  @param obj 关联对象 *  @param key 关联Key */
- (void)objc_getAssociatedObject:(id)obj key:(const void *)key {
    id object = objc_getAssociatedObject(obj,key);
    NSLog(@"%s %s %s %@",__func__,object_getClassName(obj),key,object);
}

/** * 获取所有已注册类信息 */
- (void)objc_copyClassList{
    unsigned int count = 0;

    Class *classes = objc_copyClassList(&count);

    for (unsigned int i = 0; i < count; i++) {
        Class class = classes[i];
        NSLog(@"%s%s",__func__,class_getName(class));
    }
}

/** * 获取运行时所知道的所有协议的数组(打印大量数据) */
- (void)objc_copyProtocolList {
    unsigned int count = 0;
    Protocol **protocolList = objc_copyProtocolList(&count);
    for (int i = 0; i < count; i++) {
        Protocol *protocol = protocolList[i];
        NSLog(@"%s%s",__func__,[self protocol_getName:protocol]);
    }

}

#pragma mark - Objc set: 实例变量;成员变量;协议
/** * 设置指定实例指定名称的成员变量的值 * *  @param obj 对象 *  @param name 成员变量名 *  @param value 值 */
- (void)object_setInstanceVariable:(id)obj name:(const char *)name value:(void *)value {
    Ivar ivar = object_setInstanceVariable(obj,name,value);
    NSLog(@"%s %s %@",__func__,[self ivar_getName:ivar],object_getIvar(obj, ivar));
}


/** * 设置指定对象的指定的成员变量的值 * *  @param obj 对象 *  @param ivar 成员变量 *  @param value 值 */
- (void)object_setIvar:(id)obj ivar:(Ivar)ivar value:(void *)value {
    object_setIvar(obj,ivar,value);
    NSLog(@"%s %s %@ ",__func__,[self ivar_getName:ivar],object_getIvar(obj, ivar));
}

/** * 设置关联对象的值 * *  @param obj 对象 *  @param key 关联Key *  @param value 值 *  @param policy 对象引用方式 */
- (void)objc_setAssociatedObject:(id)obj key:(const void*)key value:(id)value policy:(objc_AssociationPolicy)policy{
    objc_setAssociatedObject(obj, key, value, policy);
    [self objc_getAssociatedObject:obj key:key];
}

#pragma mark - Objc 动态创建/销毁类、对象、协议
- (void)object_dispose:(id)objc {
    Person *tempPerson = [[Person alloc] init];
    tempPerson.name = @"tempName";
    object_dispose(tempPerson);
    // 释放后 无法取得name的值
    NSLog(@"%s release result %@",__func__,tempPerson.name);
}

/** * 在指定位置创建类实例 * *  @param class 类名 *  @param bytes 位置 */
- (void)objc_constructInstance:(Class)class bytes:(void *)bytes {
    Person *obj = objc_constructInstance(class,bytes);
    obj.name = @"objc_constructInstance_name";
    NSLog(@"%s %@",__func__,obj.name);
}

/** * 销毁类实例 * *  @param obj 销毁对象 */
- (void)objc_destructInstance:(id)obj {
    Person *tempPerson = [[Person alloc] init];
    tempPerson.name = @"tempName";
    // 释放后 无法取得name的值
    objc_destructInstance(tempPerson);
    NSLog(@"%s destruct result %@",__func__,tempPerson.name);
}

/** * 创建一个新类和元类 * *  @param superclass 父类 *  @param name 类名 *  @param size 额外大小 一般给0就好了 */
- (Class)objc_allocateClassPair:(Class)superclass name:(const char*)name size:(size_t)size {
    Class newClass = objc_allocateClassPair(superclass,name,size);
    NSLog(@"%s creat new class named %@",__func__,NSStringFromClass(newClass));
    return newClass;
}

/** * 销毁一个类及其相关联的类 * *  @param class 类名 */
- (void)objc_disposeClassPair:(Class)class {
    objc_disposeClassPair(class);
    NSLog(@"%s disposs class named %@",__func__,NSStringFromClass(class));

}

/** * 在应用中注册由objc_allocateClassPair创建的类 * *  @param class 类名 */
- (void)objc_registerClassPair:(Class)class {
    objc_registerClassPair(class);
    NSLog(@"%s register class named %@",__func__,NSStringFromClass(class));
}

/** * 创建新的协议实例 * *  @param name 协议名称 * *  @return 协议实例 */
- (Protocol *)objc_allocateProtocol:(const char *)name {
    // 检查该协议是否已经注册
    if (![self class_conformsToProtocol:SelfClass protocol:NSProtocolFromString([NSString stringWithUTF8String:name])]) {
        Protocol *protocol = objc_allocateProtocol(name);
        NSLog(@"%s creat protocol named %@",__func__,NSProtocolFromString([NSString stringWithUTF8String:name]));
        return protocol;
    }
    return nil;
}

/** * 在运行时中注册新创建的协议 * *  @param protocol 协议 */
- (void)objc_registerProtocol:(Protocol *)protocol {
    // 检查该协议是否已经注册
    if (protocol) {
        objc_registerProtocol(protocol);
        NSLog(@"%s register protocol named %s",__func__,[self protocol_getName:protocol]);
    }

}

/** * 移除该对象所有关联的的对象 * *  @param object 对象 */
- (void)objc_removeAssociatedObjects:(id)object {
    objc_removeAssociatedObjects(object);
}

你可能感兴趣的:(ios,ios,object,Runtime)