swiftnsmutableattributedstringswift4.2

Swift 4.2: [Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector


I'm facing a NSInvalidArgumentException exception after upgrading a project to Swift 4.2 (conversion from 4.0).

2018-09-19 15:37:33.253482+0100 <redacted>-beta[3715:1010421] -[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290
2018-09-19 15:37:33.254312+0100 <redacted>-beta[3715:1010421] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290'

It's something related with a NSMutableAttributedString and the code is adding some attributes, for example, a NSAttributedString.Key.underlineStyle.

The exception happens when the attributed string is about to be assigned: textView.attributedText = mutableAttributedString.


UPDATE:

The code was working in Swift 4: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.styleNone.rawValue, range: range)

But with Swift 4.2, the compiler suggests changing the NSUnderlineStyle.styleNone.rawValue to NSUnderlineStyle.none.rawValue

After accepting the change, the compiler started complaining about "'none' is unavailable: use [] to construct an empty option set":

mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.none.rawValue, range: range)
                                                                                   ^~~~~ 'none' is unavailable: use [] to construct an empty option set

Solution

  • I just found out what was wrong in the code.

    So, because of the compiler error "'none' is unavailable: use [] to construct an empty option set", I replaced the NSUnderlineStyle.none.rawValue with [] 🤦‍♂️ And that's not the right case because it was using the rawValue, not the type none.

    So, the fix is using 0.

    Wrong: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: [], range: range)

    Right: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 0, range: range)