Swift-3错误:' – :无法识别的select器

以下代码与旧版本很好的结合在一起。 这是String的扩展

func stringByConvertingHTML() -> String { let newString = replacingOccurrences(of: "\n", with: "<br>") if let encodedData = newString.data(using: String.Encoding.utf8) { let attributedOptions : [String: AnyObject] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8 as AnyObject ] do { let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) //Crash here return attributedString.string } catch { return self } } return self } 

但在迅速3崩溃说

***由于未捕获的exception“NSInvalidArgumentException”而终止应用程序,原因:' – [_ SwiftValue unsignedIntegerValue]:无法识别的select器发送到实例0x6080002565f0'

任何人都请build议我需要做什么?

我遇到了同样的问题:

 let attributedOptions : [String: AnyObject] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8 as AnyObject ] 

这里的String.Encoding.utf8types检查失败。 使用NSNumber(value: String.Encoding.utf8.rawValue)

在Swift3中,不再需要强制转换为AnyObject,也不需要NSNumber。

 let attrs: [String: Any] = [ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue ] 

这篇文章保存了我的一天。 迁移到Swift 3之后,将String.Encoding.utf8改为String.Encoding.utf8.rawValue修改了这里报告的陷阱。

信号线:

 ... options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8], ... 

变成

 options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],