Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

ios

swift

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.

like image 828
Jan Válek Avatar asked Nov 14 '17 09:11

Jan Válek


2 Answers

Use this line only:

cell.lblHeader.attributedText = try NSAttributedString(data: htmlData, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
like image 124
RoyBS Avatar answered Oct 08 '22 22:10

RoyBS


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
        ]
like image 23
Serj Rubens Avatar answered Oct 08 '22 22:10

Serj Rubens