3

我刚刚更新到 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”

4

1 回答 1

4

在 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 对象

于 2017-10-13T14:38:21.767 回答