Mã sau hoàn toàn hoạt động với tính năng nhanh cũ. Đây là một phần mở rộng của chuỗiLỗi Swift-3: '- [_ SwiftValue unsignedIntegerValue]: bộ chọn không được nhận dạng
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
}
Nhưng trong nhanh chóng 3 nó bị treo nói
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x6080002565f0'
Bất cứ ai tôi xin đề nghị những gì cần phải làm gì?
Nhờ nó làm việc tốt. Nhưng nó sẽ là 'NSNumber (giá trị: String.Encoding.utf8.rawValue)' –
Lifesaver! (PS: Cũng cần NSNumber (..) để làm việc, bạn có thể plz cập nhật câu trả lời để bao gồm thay vào đó?) – Marchy
Bạn chỉ cần 'String.Encoding.utf8.rawValue', vì Swift sẽ tự động chuyển đổi' Int ' và 'UInt' vào 'NSNumber' khi một từ điển Swift được chuyển tới một hàm mong đợi một' NSDictionary'. Mặc dù điều đó đòi hỏi phải có từ điển nhanh chóng là một mảng '[String: Any]'. Xem thêm [this] (https://developer.apple.com/swift/blog/?id=39) mục nhập blog Swift. – MaddTheSane