views:

425

answers:

3

I'm new to Cocoa, and I've been having a problem that is absolutely maddening. I'm trying to write a simple NSString to an NSTextView, and for some reason it just won't respond. No compiler warnings, no runtime errors, just a blank textview.

I'm able to use other methods of NSTextView like selectAll, delete, setSelectedRanges, etc. They work fine, and from everything I've read in Apple's Docs and on the net, methods like setString should work. I've also tried:

[[textView textStorage] setAttributedString:aString]

using an NSAttributedString version of the text, again with no result. Even the string method works perfectly and returns the contents of the textView, but for some reason setString and the like won't. The TextView is editable. In debugger the value of the string I'm trying to write shows up without a problem.

I've searched everywhere on the 'net, and it seems that no-one else is having this problem.

HELP!!

Thanks, Joe

+1  A: 

Are you trying to set the string value while the field is actively being edited? While a text field is focused the control that you are actually interacting with is its editor (an NSTextView), so changes to the storage of the text field won't have any effect.

smorgan
A: 

Ensure that you are invoking -setString: on the main thread. AppKit is not thread-safe.

retainCount
A: 

I've figured it out--I'm not sure if this was what retainCount meant, but it seems I was sending the message to textView before it was initialized. I moved my -setString statement to the awakeFromNib method and it worked. I had it in the -init method in my windowController. OOps!

Joe Atkin