ios13 不允许valueForKey、setValue: forKey获取和设置私有属性

    //修改ios13 不允许valueForKey、setValue: forKey获取和设置私有属性
    //[self setValue:[UIColor orangeColor] forKey:@"view.backgroundColor"];
    //通过使用ivar获取指定名称的成员变量
    //注意成员变量必须“_名称”
    Ivar ivar = class_getInstanceVariable([self class], "_view");
    // 获取某个对象成员变量的值
    UIView *v = object_getIvar(self, ivar);
    v.backgroundColor = [UIColor redColor];
    NSString * name_type = @(ivar_getTypeEncoding(ivar));
    NSString *name_str = @(ivar_getName(ivar));
    NSLog(@"name_type:%@",name_type);
    NSLog(@"name_str:%@",name_str);
    if ([name_type isEqualToString:@"@\"UIView\""]) {
        
    }

    Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel = object_getIvar(searchTextField, ivar);
    placeholderLabel.textColor = TK999999;

unsigned  int count = 0;
    Ivar *members = class_copyIvarList([Person class], &count);
    
    for (int i = 0; i < count; i++)
    {
        Ivar var = members[i];
        const char *memberAddress = ivar_getName(var);
        const char *memberType = ivar_getTypeEncoding(var);
        NSLog(@"address = %s ; type = %s",memberAddress,memberType);
    }
    
    //对私有变量的更改
    Ivar m_address = members[1];
    object_setIvar(onePerson, m_address, @"朝阳公园");
    NSLog(@"second time : %@",[onePerson description]);

你可能感兴趣的:(ios13 不允许valueForKey、setValue: forKey获取和设置私有属性)