SWIFT: Save NSAttributedString into text file

To save NSAttributedString string into text file we convert first NSAttributedString into Data and afterwards save Data as usual into a text file:

do {
   let rtfData = try attributedText.data(from: .init(location: 0, length: attributedText.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
   try rtfData.write(to: fileURL, options: .atomic)
} catch {
   print(error)
}