iOS bug处理记录1(crash)

it2025-02-18  3

我们不光是bug的搬运工,还无时无刻在生产bug。。。(持续更新)

开发环境

Xcode 12.0.1,iOS 13.5.1(真机)

crash描述

代码用模拟器和连线用真机跑不会出问题,但是在拔掉线之后,清理后台,重启App,在一次跳转页面crash,100%复现。

处理

由于没法在控制台看日志信息,只能去看手机上保存的log了。 iOS实时查看App日志:Window->Devices and Simulators,打开Devices and Simulators界面,选择我们的手机,点击view device logs能看到手机中运行的进程输出的日志。如图:

选择时间对应的crash日志,可以看到:

查看离objc_exception_throw近的,并且可能是我们自己写的相关代码,初步猜测是红框部分出错。

删除掉跳转后tableview相关代码,相同操作后依旧crash。那么,锁定-[UITextField valueForKey:]和 -[NSObject+ 44424 (NSKeyValueCoding) valueForKeyPath:] + 247,层层查看代码中用到相关方法的地方,发现可疑代码段(这里用了一段别人的给UITextView加placeholder的代码):

删掉这个方法,crash消失!!!额。。。

度娘。。 valueForKeyPath crash, placeholderLabel,多次尝试后找到了这个(原文链接):

iOS13 使用以下两个方法出现crash -(void)setValue:(nullable id)value forKeyPath:(NSString *)keyPathK; -(nullable id)valueForKeyPath:(NSString *)keyPath;

搬过来的解决思路 #import <objc/runtime.h> - (void)updatePlaceholderColor { Ivar ivar = class_getInstanceVariable([textField class], "_placeholderLabel"); id placeholderLabel = object_getIvar(textField, ivar); Ivar ivar_1 = class_getInstanceVariable([placeholderLabel class], "_defaultAttributes"); NSMutableDictionary *defaultTextAttributes = object_getIvar(placeholderLabel, ivar_1); [defaultTextAttributes setObject:UIColor.redColor forKey:@"NSColor"]; // 或者以下一句 // [placeholderLabel performSelector:@selector(setTextColor:) withObject:UIColor.redColor]; }

我的代码是这样(注释掉那行出的问题,用后面三行代替):

+ (UIColor *)defaultPlaceholderColor { static UIColor *color = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ UITextField *textField = [[UITextField alloc] init]; textField.placeholder = @" "; // color = [textField valueForKeyPath:@"_placeholderLabel.textColor"]; Ivar ivar = class_getInstanceVariable([textField class], "_placeholderLabel"); id placeholderLabel = object_getIvar(textField, ivar); [placeholderLabel performSelector:@selector(setTextColor:) withObject:UIColor.redColor]; }); return color; }

至此,问题解决。。。呼。。

参考文档: iOS实时查看日志:https://www.jianshu.com/p/755666303387 valueForKeyPath无法找到对象中的属性时崩溃: https://www.jianshu.com/p/599e8020bad1 iOS 13 修改私有属性“_placeholderLabel”解决思路:https://www.jianshu.com/p/bc722fe4f619

最新回复(0)