Swift 4.2: [Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector
Asked Answered
L

1

17

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
Lovash answered 19/9, 2018 at 14:56 Comment(2)
try adding .rawValue to your underlineStyleParasitize
https://mcmap.net/q/658570/-how-to-use-nsunderlinestyle-patterndotParasitize
L
56

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)

Lovash answered 19/9, 2018 at 15:44 Comment(1)
Thanks, saved me a ton of work. Especially since the crash message is not very useful..Epochmaking

© 2022 - 2024 — McMap. All rights reserved.