【问题标题】:Swift 4.2: [Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selectorSwift 4.2:[Swift._EmptyArrayStorage _getValue:forType:]:无法识别的选择器
【发布时间】:2019-02-23 19:11:24
【问题描述】:

在将项目升级到 Swift 4.2(从 4.0 转换)后,我遇到了 NSInvalidArgumentException 异常。

2018-09-19 15:37:33.253482+0100 <redacted>-beta[3715:1010421] -[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290
2018-09-19 15:37:33.254312+0100 <redacted>-beta[3715:1010421] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent to instance 0x107e6c290'

它与 NSMutableAttributedString 和它添加一些属性的代码有关,例如 NSAttributedString.Key.underlineStyle

在即将分配属性字符串时发生异常:textView.attributedText = mutableAttributedString


更新:

所以,它在 Swift 4 中运行: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.styleNone.rawValue, range: range)

然后,在 Swift 4.2 中,编译器建议将 NSUnderlineStyle.styleNone.rawValue 更改为 NSUnderlineStyle.none.rawValue

之后,编译器开始大喊“'none' 不可用:使用 [] 构造一个空的选项集”:

mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.none.rawValue, range: range)
                                                                                   ^~~~~ 'none' is unavailable: use [] to construct an empty option set

【问题讨论】:

标签: swift nsmutableattributedstring swift4.2


【解决方案1】:

我刚刚发现代码有什么问题。

所以,由于编译器错误“'none' is available: use [] toconstruct an empty option set”,我把NSUnderlineStyle.none.rawValue换成了[] ?‍♂️ 这不是正确的情况,因为它使用的是rawValue,而不是none 类型。

因此,修复方法是使用 0

错误: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: [], range: range)

右: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 0, range: range)

【讨论】:

  • 谢谢,为我节省了大量工作。特别是因为崩溃消息不是很有用..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2018-12-28
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2017-01-31
相关资源
最近更新 更多