Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Most of Cocoa is available to you as classes. Cocoa has hundreds of different classes that do things like display web pages, record video, create PDFs, and search for files on the user’s computer. The process of becoming a better Cocoa programmer is really about learning how these classes work. There are some C functions and structs, too, but Cocoa is designed as an object-oriented framework.
For example, I can make a window object using the NSWindow class (I’ll tell you more about windows in Chapter 8). In three lines of code, I can create a window object, set its size and position, and display it on the screen:
id myWindow = [[NSWindow alloc] init]; [myWindow setFrame:NSMakeRect(100, 100, 400, 300) display:YES]; [myWindow orderFront:nil];
I can also set properties on the window, like the title:
myWindow.title = @"An Empty Space";
Once I’ve done that, I can see the window on the screen (Figure 4-1).
A lot of the code you write in Objective-C is very similar to this. You create an object, set some properties on it, and then the user can start interacting with the object on screen. I’ll introduce you to many different Cocoa classes as you continue through the book.