3

Je viens de mettre à jour à Xcode 9 et a converti mon application de 3 rapide à 4 rapide et obtenir ces erreurs. Comment puis-je résoudre ce problème?Je ne peux pas convertir la valeur de type 'NSAttributedString.DocumentAttributeKey' en type de clé de dictionnaire attendu 'NSAttributedString.DocumentReadingOptionKey'

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 
     } 
    } 

Et obtenir cette erreur

Impossible de convertir la valeur de type 'NSAttributedString.DocumentAttributeKey' type clé dictionnaire attendu 'NSAttributedString.DocumentReadingOptionKey'

Répondre

2

En rapide 4 - NSAttributedString la représentation est complètement changée.

Remplacez votre clé de dictionnaire d'attribut et de valeur [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType] avec [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf]

Essayez ceci:

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 
     } 
    } 
} 

Voici la note d'Apple: NSAttributedString - Creating an NSAttributedString Object