简体   繁体   English

Swift-3错误:' - [_ SwiftValue unsignedIntegerValue]:无法识别的选择器

[英]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.

相关问题 Swift 3 错误:[_SwiftValue pointSize] 无法识别的选择器发送到实例 - Swift 3 error: [_SwiftValue pointSize] unrecognized selector sent to instance Swift 3:-[_SwiftValue mergeType]:无法识别的选择器发送到实例 - Swift 3: -[_SwiftValue mergeType]: unrecognized selector sent to instance -[_ SwiftValue encodeWithCoder:]:无法识别的选择器已发送到实例 - -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance [__SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例 - [__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 使用 [__SwiftValue set] 将 AppDelegate 从 Objective-C 转换为 Swift 后,应用程序在每个 segue 上崩溃:无法识别的选择器发送到实例 - App crash on every segue after converting AppDelegate to Swift from Objective-C with [__SwiftValue set]: unrecognized selector sent to instance 未捕获的异常&#39;NSInvalidArgumentException&#39;,原因: - [_ SwiftValue floatValue]:无法识别的选择器发送到实例 - Uncaught exception 'NSInvalidArgumentException', reason: -[_SwiftValue floatValue]: unrecognized selector sent to instance Swift 4:无法识别的选择器发送到实例ERROR - Swift 4: unrecognized selector sent to instance ERROR 错误。 无法识别的选择器。 (迅速) - Error. Unrecognized selector to instance. (Swift) Swift无法识别的选择器发送到实例错误 - Swift unrecognized selector sent to instance error - [_ SwiftValue integerValue]:无法识别的选择器发送到实例0x60000044d560使用Google Mobile Vision时出错 - -[_SwiftValue integerValue]: unrecognized selector sent to instance 0x60000044d560 Error when using Google Mobile Vision
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM