My iOS app is crashing on iOS 8.2 and working perfectly on iOS 9 and above.
When I checked the crash log, the following function causes the crash (Written in the NSMutableAttributedString
extension)
func changeFont(font: UIFont, range : NSRange, keepFontSize: Bool = false)
{
let traits = font.fontDescriptor().symbolicTraits
let descriptor = baseDescriptor.fontDescriptorWithSymbolicTraits(traits)
let newFont = UIFont(descriptor: descriptor, size: keepFontSize ? descriptor.pointSize : baseDescriptor.pointSize)
self.removeAttribute(NSFontAttributeName, range: range)
self.addAttribute(NSFontAttributeName, value: newFont, range: range) // <- Crash happens here
}
Crash Log
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString addAttribute:value:range:: nil value'
When I debugged and logged the variables used in that function, the descriptor
& newFont
both shown as <uninitialized>
(lldb) po traits
▿ UIFontDescriptorSymbolicTraits
- rawValue : 268435458
(lldb) po baseDescriptor
UICTFontDescriptor <0x7fc075b574c0> = {
NSFontNameAttribute = CenturyGothic;
NSFontSizeAttribute = 14;
}
(lldb) po descriptor
<uninitialized>
(lldb) po keepFontSize
true
(lldb) po newFont
<uninitialized>
I can't use if let
or newFont != nil
check here, because both will throw an error saying "UIFont won't be nil".
How can I fix this crash ? (Or How can I check an object is initialized or not ?) I'm really stuck on this issue, since majority of my users still use iOS 8.