I am trying to read the contents of an RTF file in an attribute line, but attributedTextthere 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
}
}
Update . I changed the 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 a failure on textView.attributedText = attributedTextsaying fatal error: unexpectedly found nil while unwrapping an Optional value. In the debugger, I see that attributedTextit is not equal to zero and contains text with attributes from the file.
source
share