Transform an NSAttributedString to plain text

If I understand you correctly you have an NSData, say data, containing an encoded NSAttributedString. To reverse the process:

NSAttributedString *nas = [[NSAttributedString alloc] initWithData:data
                                                           options:nil
                                                documentAttributes:NULL
                                                             error:NULL];

and to get the plain text without attributes you then do:

NSString *str = [nas string];

With Swift 5 and macOS 10.0+, NSAttributedString has a property called string. string has the following declaration:

var string: String { get }

The character contents of the receiver as an NSString object.

Apple also states about string:

Attachment characters are not removed from the value of this property. [...]


The following Playground code shows how to use NSAttributedString's string property in order to retrieve the string content of an NSAttributedString instance:

import Cocoa

let string = "Some text"
let attributes = [NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single]
let attributedString = NSAttributedString(string: string, attributes: attributes)

/* later */

let newString = attributedString.string
print(newString) // prints: "Some text"
print(type(of: newString)) // prints: String

Updating for Swift 5:

attributedText.string