Не удается преобразовать значение типа «NSAttributedString.DocumentAttributeKey» в ожидаемый тип ключа словаря «NSAttributedString.DocumentReadingOptionKey»

Я только что обновился до Xcode 9 и преобразовал свое приложение из Swift 3 в Swift 4 и получил эти ошибки. как я могу это исправить?

 func displayText() {
    do {
        if url.pathExtension.lowercased() != "rtf" {
            let fileContent = try String(contentsOf: url)
            text.text = fileContent
        } else { // Is RTF file
            let attributedString = try NSAttributedString(url: url, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
            text.attributedText = attributedString
            text.isEditable = false
        }
    }

И получить эту ошибку

Не удается преобразовать значение типа «NSAttributedString.DocumentAttributeKey» в ожидаемый тип ключа словаря «NSAttributedString.DocumentReadingOptionKey»


person Community    schedule 13.10.2017    source источник


Ответы (1)


В swift 4 представление NSAttributedString полностью изменено.

Замените ключ и значение словаря атрибутов [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType]на [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf]

Попробуй это:

func displayText() {
    do {
        if url.pathExtension.lowercased() != "rtf" {
            let fileContent = try String(contentsOf: url)
            text.text = fileContent
        } else { // Is RTF file
            let attributedString = try NSAttributedString(url: url, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf], documentAttributes: nil)
            text.attributedText = attributedString
            text.isEditable = false
        }
    }
}

Вот примечание от Apple: NSAttributedString — Создание объекта NSAttributedString

person Krunal    schedule 13.10.2017