3

I have discovered that my App crashes whenever I add an attribute that underlines the text in an NSAttributedString. The documentation of NSAttributedStringKey.underlineStyle clearly states that the value should be a constant from NSUnderlineStyle. By simply putting the following code in a playground you instantly see an error.

import UIKit

let underlinedAttributes: [NSAttributedStringKey: Any] = [
    NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle
]

let attributedString = NSAttributedString(string: "Test", attributes: underlinedAttributes)

Error: [_SwiftValue encodeWithEncoder:] unrecognized selector sent to instance 0x6040000451...

Is this a bug or is there a solution?

Alex
  • 731
  • 1
  • 11
  • 24
  • 5
    `NSUnderlineStyle.styleSingle.rawValue` instead of `NSUnderlineStyle.styleSingle`. – Larme Sep 22 '17 at 12:34
  • It works, but that is just really ugly syntax. Well well.. Thanks for the help :) I just assumed that they had made the syntax prettier. – Alex Sep 22 '17 at 12:40
  • 1
    Do you know if this applies generally for all values in the attributes dictionaries? – Alex Sep 22 '17 at 12:41
  • 1
    You only need to apply `.rawValue` if the attribute value type is an enum. – rmaddy Sep 22 '17 at 15:47
  • That is what I meant. So the `.rawValue` is needed to get an NSNumber for the value then I guess? – Alex Sep 25 '17 at 09:23

0 Answers0