« Q&A: March 04, 2001

Posted by Andy Monitzer on March 04, 2001 [Feedback (0) & TrackBack (0)]

// Question One

I would like an NSTextField to respond to each keystroke (letter) as they are entered. For example - I have a search function that will be called as each letter is entered into the textfield, rather than waiting for the return key.


With NSTextView, you can just respond to the textDidChange delegate. It seems with NSTextField it's a little more complicated through. Perhaps you could subclass NSTextField, override textDidChange:(NSNotification *)aNotification, and send your own delegate message, and then call [super textDidChange...]

answer by: Richard Schreyer

For NSTextFields and other textual controls, you can just have the delegate implement controlTextDidChange: or register as an observer of NSControlTextDidChangeNotification. No need to be tricky about it.

answer by: Mike Ferris

// Question Two

I need to store straight data to a document. Preferably I'd like to be able to read and write NSObjects to a file. Like NSUserDefaults setObject:forKey: does. How do I do this or what class should I look at?


You have 3 options here:

  • Implementing NSCoding for your objects and using NSArchiver to store the files
  • Providing methods to convert your objects to and from property lists (key-value pairs) and storing it in a file (ASCII, XML).
  • Using a custom format

Using NSCoding is somehow easier to implement, is faster and uses less disk space (binary). More, it gives you access to additional capabilities (such as versioning). But archives are in Apple proprietary format which makes them unusable with GNUStep, for example, or from another environment than Cocoa. They are cross-platform, as long as you use Apple's implementation.

I would suggest you try this first. Just have a look at NSCoder and NSArchiver doc.

answer by: Raphael Sebbe

// Question Three

Anyone know what an error type 11 (SIGSEGV) is?


'Segmentation fault'. Maybe you accessed a deallocated object or uninitialized pointer?


NSString *string=[[NSString alloc] init]; // reference count = 1
[string release]; // reference count = 0, string is deallocated
[string release]; // BOOM


NSMutableString *string;
[string setString:@"hello"]; // BOOM

answer by: Andreas Monitzer

// Question Four

Did Apple provide Cocoa interfaces for <insert your favourite Carbon technology here> yet?

Mostly no, but generally, you can call all Carbon functions by including the Carbon framework into your project. However, you can't display Carbon controls (like Quickdraw or Java AWT/Swing) in a Cocoa window. This will hopefully be fixed sometime.

answer by: Andreas Monitzer

Post a comment