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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47281756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:26:09  来源:igfitidea点击:

Xcode 9.1 Swift 4, unable to compile with NSDocumentTypeDocumentAttribute if "if #available" is used

iosswiftxcode

提问by Jan Válek

Hi I have app with is targeting iOS8.2 in deployment target setting. I tried to convert app to swift 4 from swift3. It works, but is not working in simulator of iPhone 5s iOS 8.4. Problem is this:

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

cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

So i tried this:

所以我试过这个:

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)
}

But I'm unable to compile this code Xcode is displaying exception for iOS 8.4 branch:

但我无法编译此代码 Xcode 显示 iOS 8.4 分支异常:

Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'

I have opinion about setting base sdk to 8.x but I don't found ho to do it. In build setting I'm able to set base sdk only to 11.x version.

我对将基础 sdk 设置为 8.x 有意见,但我没有找到这样做的机会。在构建设置中,我只能将基础 sdk 设置为 11.x 版本。

I will be thankful for every idea.

我会感谢每一个想法。

回答by RoyBS

Use this line only:

仅使用此行:

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

回答by Serj Rubens

Seems like thats a correct syntax for Swift 4.1.2:

似乎这是Swift 4.1.2的正确语法

let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
            NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
            NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
        ]

回答by Zgpeace

It works in swift 5.

它适用于 swift 5。

    let htmlString = """
    <html>
      <head><style type=\"text/css\">body { font-family: Mehr Nastaliq Web; font-size: 22pt; white-space: pre-wrap; text-align: right; lang: en; direction: RTL; -webkit-user-select: none; meta charset=\"UTF-8\" }</style> </head>
      <body leftmargin=\"20\" topmargin=\"0\" rightmargin=\"20\">hello world! Arabic.?????? ????? ?? ???? ?????? ????? ?? ???? ????? ?? ????? ????? ???? ???? ????? ?? ??? ????? </body>
   </html>
    """

    let attributedString = try? NSAttributedString(data:
        htmlString.data(using: String.Encoding.unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

回答by Andrey Gerashchenko

It works great with Swift 4.0:

它适用于 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])