Build errors part II - Example in Oreilly book

aloeppert

Registered
I was playing around with the first simple windowing example in the oreilly cocoa book. The example simply contains a text field and the programmer adds a subclass of NSObject to "control" the text field. The end result is the date, displayed in the text field. Anyways, the interface builder creates some skeleton obj-c and header file. In the header file there is a something like (I don't have the code right now, it's on my home computer):
IBOutlet id textField;

and the book recommends changing the type of textField to reflect the fact that it's an NSTextField. Well, when I do that the compiler complains that it can't statically allocate the object. If I leave the code as it's generated by interface builder, then it works fine. Why doesn't this work? Sorry I can't type the exact error of the compiler, but my connection at home is down right now, but it's all in the cocoa book, for those that have it.

Thanks,
Anthony
 
Is it typed as a pointer?

The declaration needs to be

NSTextField *textField;

The asterisk is important.

Learning Cocoa has lots of errors, you're better off with Vermont Recipes at stepwise
 
Changing the name and adding the asterik is called static typing. Leaving the "IBOutlet id" as default is called dynamic typing.

This is something that makes Objective-C different from many other programming languages because it performs the link at runtime instead of compiling. This allows more flexibility for more advanced projects when you do not know what the object is that you are going to be making adjustments to.

You do not have to change the id to the NSObject, but it helps you as you are learning. Static typing speeds up the running of the program slightly (unless you had a very slow machine, or a lot of objects you will not notice).
 
Back
Top