3

Ich habe gerade Xcode 9 aktualisiert und meine App von 'swift 3' in 'swift 4' konvertiert und erhalte diese Fehler. Wie kann ich das beheben?Wert des Typs 'NSAttributedString.DocumentAttributeKey' kann nicht in den erwarteten Wörterbuchschlüsseltyp 'NSAttributedString.DocumentReadingOptionKey' konvertiert werden.

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

Und diesen Fehler

Wert kann nicht vom Typ 'NSAttributedString.DocumentAttributeKey' zu erwarten Wörterbuch Schlüsselart

Antwort

2

In schnellen 4 'NSAttributedString.DocumentReadingOptionKey' konvertieren - NSAttributedString Darstellung wird komplett geändert.

Ersetzen Sie Ihr Attribut Wörterbuch Schlüssel und Wert [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType] mit [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.rtf]

Versuchen Sie folgendes:

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

Hier Notiz von Apple ist: NSAttributedString - Creating an NSAttributedString Object

Verwandte Themen