iOS底层系列之<22>--Runtime(九)runtime类的API和成员变量的API

1、runtime类常用的API

#import "ViewController.h"
#import "Person.h"
#import <objc/runtime.h>
#import "Car.h"
#import "Cat.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    Person *person = [[Person alloc] init];
    [person run];
    
    // 1、获取类对象,获取isa指向的类
    NSLog(@"1、获取类对象%p %p", object_getClass(person),[Person class]);
    // 2、获取元类对象
    NSLog(@"2、获取元类对象%p %p", object_getClass([Person class]),[Person class]);
    
    // 3、设置类对象,即修改isa的指向
    object_setClass(person, [Car class]);
    [person run];
    
    // 4、判断是不是一个类
    NSLog(@"4、判断是不是一个类%d %d %d",object_isClass(person),object_isClass([Person class]),object_isClass(object_getClass([Person class])));
    // 5、判断是不是一个元类
    NSLog(@"5、判断是不是一个元类%d %d ",class_isMetaClass([Person class]),class_isMetaClass(object_getClass([Person class])));
    
    // 6、获取父类
    NSLog(@"6、获取父类 %@",class_getSuperclass([Person class]));
    
    // 7、动态创建一个类
    Class newClass = objc_allocateClassPair([NSObject class], "Dog", 0);
    // 添加成员变量
    class_addIvar(newClass, "_age", 4, 1, @encode(int)); // 注册之前可以添加,注册之后不能添加,因为它是只读的
    class_addIvar(newClass, "_weight", 4, 1, @encode(int));
    
    // 添加方法
    Cat *cat = [[Cat alloc] init];
    
    Method otherMethod = class_getInstanceMethod(object_getClass(cat), @selector(sleep));
    IMP meImp = method_getImplementation(otherMethod);
    BOOL isAdded = class_addMethod(newClass, @selector(sleep), meImp, "v@:"); // read-write,随时可以添加的
    // 8、注册一个类
    objc_registerClassPair(newClass);
    id dog = [[newClass alloc] init];
    
    int sizeCls = (int)class_getInstanceSize(newClass); // 获取实例对象内存大小
    
    NSLog(@"class_getInstanceSize-%d",sizeCls);
    
    // 因为没有set方法,通过kvc进行赋值
    [dog setValue:@10 forKey:@"_age"];
    [dog setValue:@20 forKey:@"_weight"];
    
    NSLog(@"age-%@ weight-%@",[dog valueForKey:@"_age"], [dog valueForKey:@"_weight"]);
    
    if (isAdded) {
        [dog sleep];
    } else {
        // 方法没添加成功
    }
    // 9、销毁一个类
    dog = nil; // 必须要先销毁实例对象,才可以调用下面的方法
    objc_disposeClassPair(newClass);
}

@end

打印结果:
2022-01-17 16:51:53.056268+0800 super-interview[95753:3650055] -[Person run]
2022-01-17 16:51:53.056380+0800 super-interview[95753:3650055] 1、获取类对象0x1030738d0 0x1030738d0
2022-01-17 16:51:53.056462+0800 super-interview[95753:3650055] 2、获取元类对象0x1030738a8 0x1030738d0
2022-01-17 16:51:53.056534+0800 super-interview[95753:3650055] -[Car run]
2022-01-17 16:51:53.056604+0800 super-interview[95753:3650055] 4、判断是不是一个类0 1 1
2022-01-17 16:51:53.056680+0800 super-interview[95753:3650055] 5、判断是不是一个元类0 1
2022-01-17 16:51:53.056757+0800 super-interview[95753:3650055] 6、获取父类 NSObject
2022-01-17 16:51:53.056839+0800 super-interview[95753:3650055] class_getInstanceSize-16
2022-01-17 16:51:53.056975+0800 super-interview[95753:3650055] age-10 weight-20
2022-01-17 16:51:53.057040+0800 super-interview[95753:3650055] -[Cat sleep]

2、成员变量的API

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *person = [[Person alloc] init];
    
    Ivar nameIvar = class_getInstanceVariable([Person class], "_name");
    Ivar ageIvar = class_getInstanceVariable([Person class], "_age");
    NSLog(@"%s %s %s",ivar_getName(nameIvar),ivar_getTypeEncoding(nameIvar),ivar_getTypeEncoding(ageIvar));
    
    object_setIvar(person, nameIvar, @"张三");
    [person setValue:@10 forKey:@"_age"];
    object_setIvar(person, ageIvar, (__bridge id _Nullable)((void *)20)); // 对int类型赋值
    NSLog(@"object_getIvar(person, ageIvar)--%@",object_getIvar(person, nameIvar));
    NSLog(@"%@",person.name);
    NSLog(@"%d",person.age);
}

打印结果
2022-01-17 17:14:49.921232+0800 super-interview[96697:3667196] _name @“NSString” i
2022-01-17 17:14:49.921348+0800 super-interview[96697:3667196] object_getIvar(person, ageIvar)–张三
2022-01-17 17:14:49.921437+0800 super-interview[96697:3667196] 张三
2022-01-17 17:14:49.921511+0800 super-interview[96697:3667196] 10

3、获取成员变量

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test3];
   
}
- (void)test3 {
    unsigned int count;
    Ivar *ivars = class_copyIvarList([Person class], &count); // 返回一个数组
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        NSLog(@"%s %s",ivar_getName(ivar),ivar_getTypeEncoding(ivar));
    }
    free(ivars); // 释放内存
}

打印结果:
2022-01-17 17:41:56.956929+0800 super-interview[97754:3683975] _age i
2022-01-17 17:41:56.957021+0800 super-interview[97754:3683975] _name @“NSString”

4、使用class_copyIvarList的场景

// 1、普通方法设置占位文本颜色
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor redColor];
    self.textF.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"请输入文字" attributes:attrs];
}
  • 这样可以是可以,但是感觉非常麻烦~
// 2、使用runtime 来修改其属性
// 思路最重要
// step1 : 应该先看下UITextField里面的成员变量
// 通过下面这段代码查看

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test3];
}

- (void)test3 {
    unsigned int count;
    Ivar *ivars = class_copyIvarList([UITextField class], &count); // 返回一个数组
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        NSLog(@"属性名:%s ",ivar_getName(ivar));
    }
    free(ivars); // 释放内存
}

打印结果:
2022-01-17 18:02:57.664960+0800 super-interview[98589:3697256] 属性名:_minimumFontSize
2022-01-17 18:02:57.665080+0800 super-interview[98589:3697256] 属性名:_delegate
2022-01-17 18:02:57.665173+0800 super-interview[98589:3697256] 属性名:_background
2022-01-17 18:02:57.665237+0800 super-interview[98589:3697256] 属性名:_disabledBackground
2022-01-17 18:02:57.665325+0800 super-interview[98589:3697256] 属性名:_clearButtonMode
2022-01-17 18:02:57.665392+0800 super-interview[98589:3697256] 属性名:_leftView
2022-01-17 18:02:57.665472+0800 super-interview[98589:3697256] 属性名:_leftViewMode
2022-01-17 18:02:57.665553+0800 super-interview[98589:3697256] 属性名:_rightView
2022-01-17 18:02:57.665615+0800 super-interview[98589:3697256] 属性名:_rightViewMode
2022-01-17 18:02:57.665700+0800 super-interview[98589:3697256] 属性名:_contentCoverView
2022-01-17 18:02:57.665869+0800 super-interview[98589:3697256] 属性名:_contentCoverViewMode
2022-01-17 18:02:57.665984+0800 super-interview[98589:3697256] 属性名:_backgroundCoverView
2022-01-17 18:02:57.666112+0800 super-interview[98589:3697256] 属性名:_backgroundCoverViewMode
2022-01-17 18:02:57.666228+0800 super-interview[98589:3697256] 属性名:_foregroundViewsAlpha
2022-01-17 18:02:57.666383+0800 super-interview[98589:3697256] 属性名:_traits
2022-01-17 18:02:57.666495+0800 super-interview[98589:3697256] 属性名:_nonAtomTraits
2022-01-17 18:02:57.671245+0800 super-interview[98589:3697256] 属性名:_fullFontSize
2022-01-17 18:02:57.671344+0800 super-interview[98589:3697256] 属性名:_controlSize
2022-01-17 18:02:57.671419+0800 super-interview[98589:3697256] 属性名:_padding
2022-01-17 18:02:57.671499+0800 super-interview[98589:3697256] 属性名:_clearButton
2022-01-17 18:02:57.671578+0800 super-interview[98589:3697256] 属性名:_clearButtonOffset
2022-01-17 18:02:57.671655+0800 super-interview[98589:3697256] 属性名:_cachedDefaultClearButtonImages
2022-01-17 18:02:57.671728+0800 super-interview[98589:3697256] 属性名:_leftViewOffset
2022-01-17 18:02:57.671800+0800 super-interview[98589:3697256] 属性名:_rightViewOffset
2022-01-17 18:02:57.671877+0800 super-interview[98589:3697256] 属性名:_floatingContainerView
2022-01-17 18:02:57.672146+0800 super-interview[98589:3697256] 属性名:_contentBackdropView
2022-01-17 18:02:57.672498+0800 super-interview[98589:3697256] 属性名:_placeholderLabel
2022-01-17 18:02:57.672829+0800 super-interview[98589:3697256] 属性名:_suffixLabel
2022-01-17 18:02:57.673169+0800 super-interview[98589:3697256] 属性名:_prefixLabel
2022-01-17 18:02:57.673606+0800 super-interview[98589:3697256] 属性名:_iconView
2022-01-17 18:02:57.674021+0800 super-interview[98589:3697256] 属性名:_label
2022-01-17 18:02:57.674266+0800 super-interview[98589:3697256] 属性名:_labelOffset
2022-01-17 18:02:57.674679+0800 super-interview[98589:3697256] 属性名:_overriddenPlaceholder
2022-01-17 18:02:57.675022+0800 super-interview[98589:3697256] 属性名:_overriddenPlaceholderAlignment
2022-01-17 18:02:57.675371+0800 super-interview[98589:3697256] 属性名:_forceDisplayOverridePlaceholder
2022-01-17 18:02:57.675783+0800 super-interview[98589:3697256] 属性名:_interactionAssistant
2022-01-17 18:02:57.676206+0800 super-interview[98589:3697256] 属性名:_selectGestureRecognizer
2022-01-17 18:02:57.676617+0800 super-interview[98589:3697256] 属性名:_fieldEditor
2022-01-17 18:02:57.677058+0800 super-interview[98589:3697256] 属性名:_textContainer
2022-01-17 18:02:57.677389+0800 super-interview[98589:3697256] 属性名:_textStorage
2022-01-17 18:02:57.677686+0800 super-interview[98589:3697256] 属性名:_textLayoutController
2022-01-17 18:02:57.677946+0800 super-interview[98589:3697256] 属性名:_linkTextAttributes
2022-01-17 18:02:57.678291+0800 super-interview[98589:3697256] 属性名:_pasteController
2022-01-17 18:02:57.678618+0800 super-interview[98589:3697256] 属性名:_inputView
2022-01-17 18:02:57.678961+0800 super-interview[98589:3697256] 属性名:_inputAccessoryView
2022-01-17 18:02:57.679304+0800 super-interview[98589:3697256] 属性名:_recentsAccessoryView
2022-01-17 18:02:57.679585+0800 super-interview[98589:3697256] 属性名:_textDragDropSupport
2022-01-17 18:02:57.679868+0800 super-interview[98589:3697256] 属性名:_draggableGeometry
2022-01-17 18:02:57.680219+0800 super-interview[98589:3697256] 属性名:_textItemDiscoverer
2022-01-17 18:02:57.680699+0800 super-interview[98589:3697256] 属性名:_textFieldFlags
2022-01-17 18:02:57.680984+0800 super-interview[98589:3697256] 属性名:_firstBaselineOffsetFromTop
2022-01-17 18:02:57.681269+0800 super-interview[98589:3697256] 属性名:_lastBaselineOffsetFromBottom
2022-01-17 18:02:57.681551+0800 super-interview[98589:3697256] 属性名:_didInvalidateBaselineConstraintsOnHeightChange
2022-01-17 18:02:57.681722+0800 super-interview[98589:3697256] 属性名:_deferringBecomeFirstResponder
2022-01-17 18:02:57.681976+0800 super-interview[98589:3697256] 属性名:_preferredBorderStyle
2022-01-17 18:02:57.682072+0800 super-interview[98589:3697256] 属性名:_preferredBackgroundCornerRadius
2022-01-17 18:02:57.682240+0800 super-interview[98589:3697256] 属性名:_backgroundProvider
2022-01-17 18:02:57.682371+0800 super-interview[98589:3697256] 属性名:_metricsProvider
2022-01-17 18:02:57.682464+0800 super-interview[98589:3697256] 属性名:_textCanvasView
2022-01-17 18:02:57.682626+0800 super-interview[98589:3697256] 属性名:_cuiCatalog
2022-01-17 18:02:57.682800+0800 super-interview[98589:3697256] 属性名:_cuiStyleEffectConfiguration
2022-01-17 18:02:57.683004+0800 super-interview[98589:3697256] 属性名:_adjustsFontForContentSizeCategory
2022-01-17 18:02:57.683240+0800 super-interview[98589:3697256] 属性名:_tvUseVibrancy
2022-01-17 18:02:57.683514+0800 super-interview[98589:3697256] 属性名:_disableTextColorUpdateOnTraitCollectionChange
2022-01-17 18:02:57.683821+0800 super-interview[98589:3697256] 属性名:_pasteDelegate
2022-01-17 18:02:57.684049+0800 super-interview[98589:3697256] 属性名:_tvCustomTextColor
2022-01-17 18:02:57.684271+0800 super-interview[98589:3697256] 属性名:_tvCustomFocusedTextColor
2022-01-17 18:02:57.684427+0800 super-interview[98589:3697256] 属性名:_textDragOptions
2022-01-17 18:02:57.684580+0800 super-interview[98589:3697256] 属性名:_textDragDelegate
2022-01-17 18:02:57.684718+0800 super-interview[98589:3697256] 属性名:_textDropDelegate
2022-01-17 18:02:57.684868+0800 super-interview[98589:3697256] 属性名:_visualStyle

可以找到:_placeholderLabel 这个就是我们需要的属性名

  • 通过kvc进行修改
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // _placeholderLabel
    self.textF.placeholder = @"请输入...";
    
//    [self.textF setValue:[UIColor redColor] forKey:@"_placeholderLabel.textColor"];
    UILabel *placeH = [self.textF valueForKeyPath:@"_placeholderLabel"];
    placeH.textColor = [UIColor redColor];
    
}

报错:
2022-01-17 18:09:34.692231+0800 super-interview[98856:3702503] *** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Access to UITextField’s _placeholderLabel ivar is prohibited. This is an application bug’

  • 原因:iOS 13这样写不行了
  • 改成:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // _placeholderLabel
    self.textF.placeholder = @"请输入...";
    
//    [self.textF setValue:[UIColor redColor] forKey:@"_placeholderLabel.textColor"];
    Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeH = object_getIvar(self.textF, ivar);
    placeH.textColor = [UIColor redColor];
    
}
  • 额,看起来好像是也有点麻烦,但是没关系,思路是重点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值