Setting NSUnderlineStyle causes unrecogognized selector exception

久未见 提交于 2019-12-06 17:02:47

问题


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)
}

回答1:


Change line:

let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]

to:

let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue]



回答2:


Swift 5 version of Vlad Khambir answer,

let attributes = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue]


来源:https://stackoverflow.com/questions/46092027/setting-nsunderlinestyle-causes-unrecogognized-selector-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!