Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-3错误:'-[\u SwiftValue unsignedIntegerValue]:无法识别的选择器_Ios_Xcode_Exception_Swift3 - Fatal编程技术网

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

Ios Swift-3错误:'-[\u SwiftValue unsignedIntegerValue]:无法识别的选择器,ios,xcode,exception,swift3,Ios,Xcode,Exception,Swift3,以下代码与旧swift完美配合。这是字符串的扩展 func stringByConvertingHTML() -> String { let newString = replacingOccurrences(of: "\n", with: "<br>") if let encodedData = newString.data(using: String.Encoding.utf8) { let attributedOptions : [Strin

以下代码与旧swift完美配合。这是字符串的扩展

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
}
func stringByConvertingHTML()->String{
let newString=replacingOccurrences(of:“\n”,with:“
”) 如果让encodedData=newString.data(使用:String.Encoding.utf8){ let AttributeOptions:[字符串:AnyObject]=[ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType作为任何对象, NSCharacterEncodingDocumentAttribute:String.Encoding.utf8作为任何对象 ] 做{ 让attributedString=try NSAttributedString(数据:encodedData,选项:attributeoptions,文档属性:nil)//在这里崩溃 返回attributedString.string }抓住{ 回归自我 } } 回归自我 }
但在swift 3中,它崩溃了,说

***由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:'-[\u SwiftValue unsignedIntegerValue]:发送到实例的选择器无法识别 0x608002565F0'


有谁能告诉我需要做什么吗?

我遇到了同样的问题:

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

这里的
String.Encoding.utf8
类型检查失败。在Swift3中使用NSNumber(值:String.Encoding.utf8.rawValue)

不需要对任何对象进行强制转换,也不需要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],

.rawValue
添加到末尾…

谢谢,它工作正常。但它将是
NSNumber(值:String.Encoding.utf8.rawValue)
Lifesaver!(PS:还需要NSNumber(..)才能工作,您可以更新答案以包含它吗?)您只需要
String.Encoding.utf8.rawValue
,当Swift字典传递给需要
NSDictionary
的函数时,as Swift将自动将
Int
s和
UInt
s转换为
NSNumber
s。尽管这需要swift字典是
[String:Any]
数组。另请参阅Swift博客。有时我会收到NSRangeException,任何解决此问题的方法!但事实上,枚举应该作为rawValue发送。我认为它将在未来的版本中得到修复。我会说这是最干净的方法。我永远不会从错误注释中猜到解决方案是在最后添加.rawValue。太多了!