11

I'm trying to read RTF file contents to attributed string, but attributedText is nil. Why?

if let fileURL = NSBundle.mainBundle().URLForResource(filename, withExtension: "rtf") {
    var error: NSError?
    if let attributedText = NSAttributedString(fileURL: fileURL, options: [NSDocumentTypeDocumentAttribute:NSRTFDTextDocumentType], documentAttributes: nil, error: &error){
        textView.attributedText = attributedText
    }
}

Upd.: I changed code to:

if let fileURL = NSBundle.mainBundle().URLForResource(filename, withExtension: "rtf") {
    var error: NSError?

    let attributedText = NSAttributedString(fileURL: fileURL, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: &error)
        println(error?.localizedDescription)


        textView.attributedText = attributedText

}

Now there is crash on textView.attributedText = attributedText says: fatal error: unexpectedly found nil while unwrapping an Optional value. I see in debugger that attributedText is non nil and contains text with attributes from file.

Shmidt
  • 16,436
  • 18
  • 88
  • 136

2 Answers2

10

Rather than looking to see if the operation worked/failed in the debugger, you’d be much better off writing the code to handle the failure appropriately:

if let attributedText = NSAttributedString(fileURL: fileURL, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: &error) {
    textView.attributedText = attributedText
}
else if let error = error {                
    println(error.localizedDescription)
}

Swift 4

do {
    let attributedString = try NSAttributedString(url: fileURL, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil)
} catch {
    print("\(error.localizedDescription)")
}
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
  • There is still crash on ```Optional("The operation couldn’t be completed. (Cocoa error 256.)")```. Error is nil. – Shmidt Jan 03 '15 at 13:40
  • 1
    If you have the exact code I put above, I can’t see how error can be nil given the error you give is non-nil (and if it’s nil, where is your error message coming from?). Are you sure the crash isn’t elsewhere in your code? – Airspeed Velocity Jan 03 '15 at 13:43
  • you were right. The problem was I used didSet to set text. Replacing code to viewDidLoad solved the problem. – Shmidt Jan 03 '15 at 13:48
  • How can i get the text of odt file? – Deepak Saki Nov 14 '18 at 06:38
2

Swift 4 @available(iOS 7.0, *)

func loadRTF(from resource: String) -> NSAttributedString? {
    guard let url = Bundle.main.url(forResource: resource, withExtension: "rtf") else { return nil }

    guard let data = try? Data(contentsOf: url) else { return nil }

    return try? NSAttributedString(data: data,
                                   options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf],
                                   documentAttributes: nil)
}

Usage:

textView.attributedText = loadRTF(from: "FILE_HERE_WITHOUT_RTF")
zombie
  • 5,069
  • 3
  • 25
  • 54
  • 1
    **Swift 4** Update `options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtf]` – selljamhere Oct 13 '17 at 02:17