Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 it's 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:

So, it's was working in Swift 4: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.styleNone.rawValue, range: range)

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

After that, the compiler starts yelling "'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
like image 529
ricardopereira Avatar asked Sep 19 '18 14:09

ricardopereira


1 Answers

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)

like image 82
ricardopereira Avatar answered Nov 10 '22 15:11

ricardopereira