xcode 无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46732197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
提问by
I have just updated to Xcode 9 and converted my app from swift 3 to swift 4 and get this errors. how i can fix this?
我刚刚更新到 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
}
}
And get this Error
并得到这个错误
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”
回答by Krunal
In swift 4 - NSAttributedString representation is completely changed.
在 swift 4 - NSAttributedString 表示完全改变。
Replace your attribute dictionary key and value [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType]
with [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf]
将您的属性字典键和值[NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType]
替换为[NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf]
Try this:
尝试这个:
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
}
}
}
Here is note from Apple: NSAttributedString - Creating an NSAttributedString Object
这是 Apple 的注释:NSAttributedString - 创建一个 NSAttributedString 对象