| |||||||||
Cocoa is Apple Computer's native object-oriented application programming environment for the Mac OS X operating system. It is one of five major programming environments available for Mac OS X; the others are Carbon, Classic, BSD, and Java. (Environments such as Perl and Python are considered minor environments because they are not generally used for full-fledged application programming).
Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder. However the Cocoa programming environment can be accessed using other tools, such as Perl, Python and PyObjC.
For end-users, Cocoa applications are considered to be those written using the Cocoa programming environment. Such applications usually have a distinctive feel, since the Cocoa programming environment automates many aspects of an application to comply with Apple's Human User Interface guidelines.
Cocoa is derived from the NeXTSTEP and OPENSTEP programming environments developed by NeXT in the late 1980s. Apple acquired NeXT in December, 1996, and subsequently went to work on the Rhapsody operating system that was supposed to be the direct successor of OPENSTEP and use OPENSTEP technology proper, and have an emulation base for Mac OS applications, which was termed Blue Box. The OPENSTEP base of libraries and binary support was termed Yellow Box. Rhapsody evolved into Mac OS X, and the Yellow Box became Cocoa.
Much of the work that went into developing OPENSTEP was applied to the development of Mac OS X. Cocoa is the most visible part of that synergy. There are, however, some important fundamental differences. The most visible of which is that NeXTSTEP and OPENSTEP used Display PostScript for on-screen display of text and graphics, while Cocoa depends on Apple's Quartz (which uses PDF). Cocoa also has some level of internet support (NSUrl classes, and others), while under OPENSTEP one managed network connections through NSFileHandle classes and Berkeley sockets.
Prior to its current use, the "Cocoa" trademark was the name of an application that allowed children to create multimedia projects. It was originally known as KidSim. The program was discontinued as part of the rationalizations following Steve Jobs' return to the company.
Cocoa consists of two Objective-C libraries called frameworks. Frameworks are a construct unique to the NeXTSTEP/OpenStep/Cocoa family of programming environments. They are functionally similar to shared libraries, which are often referred to by the acronym DSO, for dynamic shared object, or DLL, for dynamically linked library. A framework consists of a compiled object that can be dynamically loaded into a program's address space at run-time, along with the associated resources, header files, and documentation.
The "NS" prefix, used for all framework objects, comes from Cocoa's NeXTSTEP/OpenStep heritage, as does the .nib file extension used by the Interface Builder. The .nib suffix originally stood for NeXT Interface Builder, but conventional wisdom now holds that .nib doesn't stand for anything; it is now said to refer to the word nib, meaning the sharpened point of a quill or pen.
One feature of the Cocoa environment that is certainly unusual, if not unique, is its facility for managing dynamically allocated memory. Cocoa's NSObject class, from which all Cocoa classes, both vendor and user, are derived, implements a reference counting scheme for memory management. Every object has a retain method and a release method, and an instance variable accessible through the retainCount accessor method. A newly allocated object, created with alloc, has a retain count of one. Sending that object a retain message increments the retain count, while sending it a release message decrements the retain count. When an object's retain count reaches zero, it is deallocated and its memory is freed. (Deallocation is to Objective C objects as destruction is to C++ objects. The dealloc method is functionally equivalent to a C++ destructor.)
In addition to manual reference counting, application programmers may choose to make use of autorelease pools. Sending an object an autorelease message registers a future release message with that thread's global autorelease pool. When the autorelease pool is deallocated, it sends the corresponding release message for every registered autorelease. Autorelease pools are generally deallocated and re-created at then end of the program's event loop, guaranteeing program flow has passed out of the block where that object was autoreleased.
Cocoa gives the programmer the choice of whether to manually manage memory management of objects or not. Opinions on this are divided. Some say that Cocoa's memory management is superior because it allows the programmer to have precise control over when his objects are deallocated, but does not burden him with the necessity of doing so for every object a program allocates. Others say that the whole mess is unnecessary, and that Java-style automatic garbage collection is superior, because it removes the possibility of programmer error in memory management.
Java bindings for the Cocoa frameworks are also available. AppleScript Studio, part of Apple's Xcode Tools makes it possible to write (less complex) Cocoa applications using AppleScript. However the frameworks themselves are written in Objective-C and hence Objective-C is presently the preferred language for the Cocoa applications.
An open source implementation of the OpenStep specification is available under the name GNUstep.
Third party bindings for other languages:
Cocoa consists of an integrated set of classes implementing a comprehensive application framework. The classes work together to implement the structure of an application in a generic manner. Two basic structures are available - a single window interface, and a multiple-document interface. In addition it is possible to develop applications with no GUI at all (foundation tools). These structures permit very rapid application development (RAD) - for example, creating a complete multiple document text editor featuring almost word-processor like levels of interface, and reading and writing to .rtf (rich text format) files takes about 20 lines of code, because classes already exist that implement complete user interfaces for text editing.
A key part of the Cocoa architecture is its comprehensive views model. This is organised along conventional lines for an application framework, but is based on the PDF drawing model provided by Quartz. This allows creation of custom drawing content using PostScript-like drawing commands, which also allow automatic printer support and so forth. Since the Cocoa framework manages all the clipping, scrolling, scaling and other chores of drawing graphics, the programmer is freed from implementing basic infrastructure and can concentrate only on the unique aspects of an application's content.
Once the basic organising principles of Cocoa are understood, creating complex applications takes a remarkably short amount of time - possibly one of the fastest methods available on any platform that yields this degree of performance and flexibility.