【问题标题】:Setting NSUnderlineStyle causes unrecogognized selector exception设置 NSUnderlineStyle 会导致无法识别的选择器异常
【发布时间】:2018-02-15 22:29:06
【问题描述】:

我正在尝试使用 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)
}

【问题讨论】:

  • underlineStyle 键需要一个整数值(特别是 NSNumber),因此您需要使用 NSUnderlineStyle rawValue

标签: swift swift3 nsattributedstring


【解决方案1】:

Swift 5 版本的Vlad Khambir 答案,

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

【讨论】:

    【解决方案2】:

    换行:

    let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]
    

    到:

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

    【讨论】:

    • 在调查并试图弄清这个问题的根源后,一旦我指出问题,我就遇到了这篇文章 - 谢谢你
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多