设置NSUnderlineStyle会导致无法识别的选择器异常

时间:2017-09-07 08:55:22

标签: swift swift3 nsattributedstring

我试图用NSAttributed字符串为字符串加下划线。出于某种原因,我的行会导致异常:

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

结果应该用于UITableView中的UILabel,并根据需要创建。

这是代码:

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

2 个答案:

答案 0 :(得分:35)

更改行:

let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]

为:

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

答案 1 :(得分:1)

Swift 5 版本的Vlad Khambir答案,

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