25

I am trying to underline a string with NSAttributed string. For some reason, my lines cause the exception:

-[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance   

The result is supposed to be used for a UILabel within a UITableView and is created as needed.

This is the code:

attributedString = NSMutableAttributedString(string: message)

if let actor = event.actor {
   let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]
   var attributedActorString = NSMutableAttributedString(string: actor.shirtName, attributes: attributes)
   attributedActorString.insert(NSAttributedString(string: " "), at: 0)
   attributedActorString.append(NSAttributedString(string: ". "))                               attributedActorString.append(attributedImageStringForUrl(event.actor!.portraitImageUrl, indexPath: indexPath))
   attributedString.append(attributedActorString)
}
ff10
  • 3,046
  • 1
  • 32
  • 55
  • 1
    `underlineStyle` key requires an integer value (specifically an `NSNumber`) so that's why you need to use the `NSUnderlineStyle `'s `rawValue`. – Jakub Truhlář Oct 27 '18 at 18:46

2 Answers2

56

Change line:

let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]

to:

let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue]
Vlad Khambir
  • 4,313
  • 1
  • 17
  • 25
  • After investigating and trying to get to the bottom of this issue, once I pin pointed the problem I came across this posts - thanks you – RichAppz May 30 '19 at 10:13
3

Swift 5 version of Vlad Khambir answer,

let attributes = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]
Kamran
  • 14,987
  • 4
  • 33
  • 51