Cocoa Programming for Mac OS X - Aaron Hillegass [12]
• YES is 1.
• NO is 0.
• IBOutlet is a macro that evaluates to nothing. Ignore it. (IBOutlet is a hint to Interface Builder when it reads the declaration of a class from a .h file.)
• IBAction is the same as void. It also acts as a hint to Interface Builder.
• nil is the same as NULL. We use nil instead of NULL for pointers to objects.
Look at the Header File
Click on RandomController.h. Study it for a moment. It declares RandomController to be a subclass of NSObject. Instance variables are declared inside the braces.
#import @interface RandomController : NSObject { IBOutlet NSTextField *textField; } - (IBAction)generate:(id)sender; - (IBAction)seed:(id)sender; @end #import is similar to the C preprocessor’s #include. However, #import ensures that the file is included only once. You are importing Note that the declaration of the class starts with @interface. The @ symbol is not used in the C programming language. To minimize conflicts between C code and Objective-C code, Objective-C keywords are prefixed by @. Here are a few other Objective-C keywords: @end, @implementation, @class, @selector, @protocol, @property, and @synthesize. Edit the Implementation File In C++ or Java, you might implement a method something like this: public void increment(Object sender) { count++; textField.setIntValue(count); } In English, you would say, “increment is a public instance method that takes one argument that is an object. The method doesn’t return anything. The method increments the count instance variable and then sends the message setIntValue() to the textField object with count as an argument.” In Objective-C, the analogous method would look like this: - (void)increment:(id)sender { count++; [textField setIntValue:count]; } Objective-C is a very simple language. It has no visibility specifiers: All methods are public, and all instance variables are protected. (In fact, there are visibility specifiers for instance variables, but they are rarely used. The default is protected, and that works nicely.) In Chapter 3, we will explore Objective-C in all its beauty. For now, just copy the following methods. You can safely remove the init and dealloc methods Xcode has created for you. #import "RandomController.h" @implementation RandomController - (IBAction)generate:(id)sender { // Generate a number between 1 and 100 inclusive int generated; generated = (int)(random() % 100) + 1; NSLog(@"generated = %d", generated); // Ask the text field to change what it is displaying [textField setIntValue:generated]; } - (IBAction)seed:(id)sender { // Seed the random number generator with the time srandom((unsigned)time(NULL)); [textField setStringValue:@"Generator seeded"]; } @end (Remember that IBAction is the same as void. Neither method returns anything.) Because Objective-C is C with a few extensions, you can call functions, such as random() and srandom() from the standard C and Unix libraries. Build and Run If your code has an error, Xcode’s status display will show that the build has failed. Select the Issue navigator to see a list of build issues. If you click on an issue, the erroneous line of code will be highlighted in the editor. In Figure 2.21, the programmer has forgotten a semicolon. The compiler is smart about certain types of errors;
Now look at RandomController.m. It contains the implementations of the methods. You can find it in the project navigator, or you can use Xcode’s Navigate -> Jump to Next Counterpart command, Control-Command-UpArrow, which flips the editor between corresponding .h and .m files. You can also enable the Assistant Editor in the toolbar, which automatically shows the counterpart for the selected file beside it.
Your application is now finished. Click Run in the toolbar to run your application again.