ios - Xcode 9.1 Swift 4,如果使用 "if#available",则无法使用 NSDocumentTypeDocumentAttribute 进行编译

标签 ios swift xcode

您好,我的应用程序在部署目标设置中以 iOS8.2 为目标。我试图将应用程序从 swift3 转换为 swift 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 sdk 设置为 8.x 有意见,但我没有找到这样做的理由。在build设置中,我只能将基本 sdk 设置为 11.x 版本。

我会感谢每一个想法。

最佳答案

仅使用这一行:

cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

关于ios - Xcode 9.1 Swift 4,如果使用 "if#available",则无法使用 NSDocumentTypeDocumentAttribute 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47281756/

相关文章:

jquery - UIWebView 中的控制台错误 "CGAffineTransformInvert: singular matrix"

ios - 将类型作为变量传递

iphone - 如何使用 iPhone 中已安装的 GPS 系统

iOS 9 新功能 Free Provisioning(在设备上运行您的应用程序,只需使用您的 Apple ID,无需 Apple 开发者成员(member)资格)

android - 如何读取 Android 和 iOS 的 GRIB 文件?

swift - 使用 AlamoFireImage 在 CollectionView 中使用 .af_setImage 方法加载数组的 url 时发生 fatal error

swift - 使用 CGAffineTransform MakeRotation 时无法增加 UIView 的高度和宽度

xcode - DVTCoreSimulatorAdditionsErrorDomain Xcode 10GM

html - 如何从网站上的链接打开 native iOS 应用程序(日历、笔记...)?

iphone - 有什么方法可以使用 iOS SDK 将图像上传到 pinterest,也许使用 fileURL?