How to find the text in an NSTextView?

Justin picture Justin · Apr 3, 2011 · Viewed 11.8k times · Source

I need to find the text in an NSTextView, and save it to a file. I can do the saving fine. I have used -stringValue, and -textStorage so far, but neither have worked. When I put -stringValue in, it just gave me (null), and when I put -textStorage in, it gave me a very large chunk of nothing (like a long paragraph of invisible text).

How can I put the text from an NSTextView into an NSString?

Answer

Nick Weaver picture Nick Weaver · Apr 3, 2011

Try

NSString *text = [[myTextView textStorage] string];

The NSTextStorage inherits from NSMutableAttributedString which inherits from NSAttributedString. The latter implements the string method.

This isn't too obvious if you just look at the NSTextView's instance methods in the documentation.