I have a NSAttributedString
object as a property of a custom object. I need to save this custom object to the disk in JSON format. Later I need to
You could use this simple code snippet to convert NSAttributedString
to XML without actually parsing NSAttributedString
. This can be a human-readable alternative to JSON if you can afford verbose text output.
It can be also used for decoding back to NSAttributedString
.
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.outputFormat = .XMLFormat_v1_0
textView.attributedText.encodeWithCoder(archiver)
archiver.finishEncoding()
let textAsString = NSString(data: data, encoding: NSUTF8StringEncoding)'