以下のコードは、古いswiftで完全に機能しました。これは、文字列Swift-3エラー: ' - [_ SwiftValue unsignedIntegerValue]:認識できないセレクタ
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
}
の拡張である。しかし、SWIFT 3には
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x6080002565f0'
誰もが何をする必要があるかを私に提案してください?と言ってクラッシュ
おかげに変更
。しかし、それは 'NSNumber(value:String.Encoding.utf8.rawValue)' –
ライフセーバー! (PS:NSNumber(..)が動作するためにも必要でしたが、代わりに答えを更新することができますか?) – Marchy
Swiftは 'Int'sを自動的に変換するので' String.Encoding.utf8.rawValue'が必要ですSwift辞書が 'NSDictionary'を期待する関数に渡されたときに' UInt'sを 'NSNumber'sに変換します。これには、素早い辞書を '[String:Any]'配列にする必要があります。 [this](https://developer.apple.com/swift/blog/?id=39)Swiftブログエントリもご覧ください。 – MaddTheSane