Swift 4.2:[Swift._EmptyArrayStorage _getValue:forType:]:无法识别的选择器

ric*_*ira 10 nsmutableattributedstring swift swift4.2

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

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'
Run Code Online (Sandbox Code Playgroud)

它是与a相关的东西NSMutableAttributedString和它添加一些属性的代码,例如,a NSAttributedString.Key.underlineStyle.

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


更新:

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

然后,使用Swift 4.2,编译器建议更改NSUnderlineStyle.styleNone.rawValueNSUnderlineStyle.none.rawValue

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

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

ric*_*ira 37

我刚刚发现代码中出了什么问题.

因此,由于编译器错误" 'none'不可用:使用[]构造一个空选项集 ",我NSUnderlineStyle.none.rawValue[]♂️ 替换了那个并不是正确的情况,因为它使用的是rawValue,而不是类型none.

所以,修复正在使用0.

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

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

  • 谢谢,节省了我很多工作。特别是因为崩溃消息不是很有用。 (2认同)