嗨,我有在部署目标设置是针对iOS8.2的应用程序。我试着将应用程序从swift3转换为from 4。它可以工作,但不能在iPhone 5s iOS 8.4的模拟器中工作。问题是:
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)所以我试了一下:
if #available(iOS 11, *){
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
}
if #available(iOS 8.4, *){
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}但是我无法编译这段代码,Xcode显示了iOS 8.4分支的异常:
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'我对将base设置为8.x有自己的看法,但我不认为应该这样做。在构建设置中,我只能将base设置为11.x版本。
我会感谢每一个想法。
发布于 2017-12-26 07:44:32
只使用这一行:
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)发布于 2018-06-20 12:42:06
这似乎是Swift 4.1.2:的正确语法
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
]发布于 2018-01-22 15:30:36
它在Swift 4.0中运行得很好:
let documentKey = NSAttributedStringKey("NSDocumentTypeDocumentAttribute")
let charsetKey = NSAttributedStringKey("NSCharacterEncodingDocumentAttribute")
let string = NSAttributedString(string: htmlString,
attributes: [documentKey: NSAttributedString.DocumentType.html,
charsetKey: String.Encoding.utf8.rawValue])https://stackoverflow.com/questions/47281756
复制相似问题