views:

45

answers:

3

Hi folks!

I have an instance of NSData that I stored from a previous run of my application. I want to set an NSTextView's text using this data. It is RTF and I can't figure out how to do this. Any ideas or suggestions? It needs to be done with code and not Interface Builder.

Thanks

+1  A: 
//First convert a NSData object to a NSString object.

NSData *aData;
//assign aData to what you want it to.
NSAttributedString *aStr;
aStr = [[[NSAttributedString alloc] initWithRTF:aData documentAttributes:NULL] autorelease];
//then set the textView to that value.

[[yourTextView textStorage] setAttributedString:aStr];
thyrgle
I assume he wants to interpret the RTF, not just display it.
JWWalker
Ok edited the answer :)
thyrgle
thanks for the response, but when I try to set the textview's attributed string I get this error*** -[NSBigMutableString replaceCharactersInRange:withString:]: nil argumentAny ideas?
bob
A: 

You can initialize an NSAttributedString using RTF data, and the NSTextStorage owned by the NSTextView derives from NSMutableAttributedString. I think you can put the pieces together from that.

JWWalker
A: 
NSData *myData; // RTF data
NSAttributedString *myString = [[NSAttributedString alloc] initWithRTFD:myData documentAttributes:NULL];

[[yourTextView textStorage] setAttributedString:myString];

thyrgle is close, but NSAttributedString needs to be initialized using

initWithRTFD: documentAttributes:
instead of
initWithRTF: documentAttributes:

Conceited Code