首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode 9.1 Swift 4,如果使用"if #available“,则无法使用NSDocumentTypeDocumentAttribute进行编译

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

Stack Overflow用户
提问于 2017-11-14 09:14:39
回答 4查看 7.6K关注 0票数 10

嗨,我有在部署目标设置是针对iOS8.2的应用程序。我试着将应用程序从swift3转换为from 4。它可以工作,但不能在iPhone 5s iOS 8.4的模拟器中工作。问题是:

代码语言:javascript
复制
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

所以我试了一下:

代码语言:javascript
复制
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分支的异常:

代码语言:javascript
复制
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'

我对将base设置为8.x有自己的看法,但我不认为应该这样做。在构建设置中,我只能将base设置为11.x版本。

我会感谢每一个想法。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-12-26 07:44:32

只使用这一行:

代码语言:javascript
复制
cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
票数 30
EN

Stack Overflow用户

发布于 2018-06-20 12:42:06

这似乎是Swift 4.1.2:的正确语法

代码语言:javascript
复制
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
            NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
            NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
        ]
票数 15
EN

Stack Overflow用户

发布于 2018-01-22 15:30:36

它在Swift 4.0中运行得很好:

代码语言:javascript
复制
let documentKey = NSAttributedStringKey("NSDocumentTypeDocumentAttribute")
let charsetKey = NSAttributedStringKey("NSCharacterEncodingDocumentAttribute")
let string = NSAttributedString(string: htmlString,
                                attributes: [documentKey: NSAttributedString.DocumentType.html,
                                             charsetKey: String.Encoding.utf8.rawValue])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47281756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档