I have an NSAttributedString that contains text and images with attributes associated to them. I'm trying to convert the NSAttributedString into an RTFD so that it can be uploaded to Cloud Firestore and then can be converted back from an RTFD into an NSAttributedString.
I'm currently using these extensions to convert from an NSAttributedString to an RTFD and back but they are returning nil.
extension NSAttributedString {
var attributedString2rtfd: String? {
do {
let rtfdData = try self.data(from: NSRange(location: 0, length: self.length), documentAttributes:[.documentType: NSAttributedString.DocumentType.rtfd])
return String.init(data: rtfdData, encoding: String.Encoding.utf8)
} catch {
print("error:", error)
return nil
}
}
}
extension String {
var rtfd2AttributedString: NSAttributedString? {
do {
guard let data = data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.rtfd,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
}
These extensions work when I replace all of the "rtfd" with "rtf" and only have text in the attributed string. However, when I include images in the attributed string and use the extensions with "rtfd," as shown above, they both return nil. What am I doing wrong here?