简体   繁体   中英

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

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:

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:

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.

I will be thankful for every idea.

仅使用此行:

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

Seems like thats a correct syntax for Swift 4.1.2:

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

It works great with 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])

It works in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM