一个 DesignablesAgent 错误的解决

本文介绍了在调试自定义视图时遇到的一个问题,即DesignablesAgent无法加载自定义字体,导致运行时错误。分析了DesignablesAgent在渲染时不会加载应用的主bundle,因此在init方法中直接加载资源可能导致nil。解决方案是避免在init中强制解包可选资源,如字体。提供了一个修正强制解包的示例代码。
摘要由CSDN通过智能技术生成

在调试 custom view 时出现错误:

Runtime: iOS 14.5 (18E182) - DeviceType: IBSimDeviceTypeiPad2x

进入 ~/Library/Logs/DiagnosticReports 目录,查看 IBDesignablesAgent-iOS 开头的日志,发现:

1   libswiftCore.dylib            	0x0000000107576509 closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 105
2   libswiftCore.dylib            	0x00000001075762a9 closure #1 in _assertionFailure(_:_:file:line:flags:) + 121
3   libswiftCore.dylib            	0x0000000107575f6c _assertionFailure(_:_:file:line:flags:) + 428
4   com.iconmobile.henkel.salonlab.debug	0x00000002082ede2e static UIFont.font(type:size:) + 302 (FontStyling.swift:14)
5   com.iconmobile.henkel.salonlab.debug	0x000000020815e6de ChooseStatusLabel.commonTextUIUpdate() + 206

代码定位到这里:

titleLabel.font = .font(type: .apercuRegular, size: 18)

原因很简单了,DesignablesAgent 加载不了自定义字体。

此类错误的一般原因都是 custom view 的 init() 方法中有错误代码,比如加载不了图片资源(IB bug)或者自定义字体。
这个 app 运行时很正常,并没有出现加载不了字体的情况。为什么 DesignablesAgent 会加载不了自定义字体 ?原因如下:

IB 在渲染 costom view 时,使用一个 DesignablesAgent 的组件。这个组件在渲染时不会加载 App 的 bundle(main bundle)。因此你在 init 方法(不管哪个init 方法)中,如果有代码加载了图片资源或custom字体,那将返回 nil,正常情况下不会有问题,因为无论是 UIImage 的 image 属性,还是 UILabel 的 font 属性,都是 optional 的。而且哪怕是 init 时加载不到,但在 updateDisplay 时还是会重新加载(如果你通过 IB 设置了这些属性),所以图片、字体还是可以正常显示的。

但是如果你对这些资源进行了强制解包(使用! 进行强制解包),比如:

	static func font(type: FontType, size: Float) -> UIFont {
        UIFont(name: type.rawValue, size: CGFloat(size))!
    }

当然就会导致 Designables Agent crashed。因此需要将强制解包修改为:

UIFont(name: type.rawValue, size: CGFloat(size)) ?? UIFont.systemFont(ofSize: CGFloat(size))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值