[英]Swift-3 error: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector
Following code was perfectly worked with old swift. 以下代码与旧swift完美配合。 This is an extension of String
这是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
}
But in swift 3 it crashes saying 但是在迅速的3中,它崩溃了
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x6080002565f0'
***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [_ SwiftValue unsignedIntegerValue]:无法识别的选择器发送到实例0x6080002565f0'
Anyone please suggest me what need to do? 有人请建议我需要做什么?
I ran into the same problem: 我遇到了同样的问题:
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType as AnyObject,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8 as AnyObject
]
Here the String.Encoding.utf8
the type check fails. 这里的
String.Encoding.utf8
类型检查失败。 Use NSNumber(value: String.Encoding.utf8.rawValue)
使用
NSNumber(value: String.Encoding.utf8.rawValue)
In Swift3 no cast to AnyObject is needed anymore and also no NSNumber. 在Swift3中,不再需要强制转换为AnyObject,也不需要NSNumber。
let attrs: [String: Any] = [
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
]
This post saved my day. 这篇文章救了我的一天。 After migrating to Swift 3, the little change
String.Encoding.utf8
to String.Encoding.utf8.rawValue
fixed the trap reported here. 迁移到Swift 3后,将
String.Encoding.utf8
改为String.Encoding.utf8.rawValue
修改了此处报告的陷阱。
Orignal line: 原始线:
...
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8],
...
changed to 变成
options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
add the .rawValue
to the end... 将
.rawValue
添加到最后......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.