iosswiftxcode

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.


Solution

  • Use this line only:

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