Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder, using the Objective-C language. However, the Cocoa programming environment can be accessed using other tools, such as Pascal, Python, Perl and Ruby, with the aid of bridging mechanisms such as PasCocoa, PyObjC, CamelBones and RubyCocoa, respectively. It is also possible to write Objective-C Cocoa programs in a simple text editor and build it manually with GCC or GNUstep's makefile scripts.
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 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. It was to have an emulation base for Mac OS applications, called 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. As a result, Cocoa classes begin with the acronym "NS" (standing either for the NeXT-Sun creation of OPENSTEP, or for the original proprietary term for the OPENSTEP framework, NeXTSTEP): NSString, NSArray, etc.
Much of the work that went into developing OPENSTEP was applied to the development of Mac OS X, Cocoa being the most visible part. There are, however, some differences. For example, NeXTSTEP and OPENSTEP used Display PostScript for on-screen display of text and graphics, while Cocoa depends on Apple's Quartz (which uses the PDF imaging model). Cocoa also has a level of Internet support, including the NSURL and WebKit HTML classes, and others, while under OPENSTEP there was only rudimentary support for 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, and is now licensed to a third party and marketed as Stagecast Creator. The program was discontinued in one of the rationalizations that followed Steve Jobs' return to Apple. The name was re-used to avoid the delay while registering a new trademark, with Stagecast agreeing to market the older Cocoa under a new name.
releasemessage and keep a retain count which can be queried by sending a
retainCountmessage. A newly allocated object created with
copyhas a retain count of one. Sending that object a
retainmessage increments the retain count, while sending it a
releasemessage decrements the retain count. When an object's retain count reaches zero, it is deallocated, its memory freed. (Deallocation is to Objective-C objects as destruction is to C++ objects. The
deallocmethod is functionally similar to a C++ destructor.
deallocis not guaranteed to be invoked.) This reference counting approach is very similar to that of Microsoft's COM which features the IUnknown interface. IUnknown provides equivalents to
releasein the form of
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 with that thread's nearest autorelease pool. When the autorelease pool is itself released, it sends a corresponding
release message for every registered
autorelease. Autorelease pools are generally created and released at the beginning and end of the event loop, guaranteeing program flow has passed out of the block where objects were autoreleased. This means the application has predictable performance and memory collection is generally invisible to the user, whereas under most fully automated schemes the application will sometimes suddenly stop responding when the garbage collection system is started.
Automatic garbage collection for Cocoa is also supported starting with Objective-C 2.0, using Xcode 3.0 which is included with Mac OS X 10.5 Leopard. The programmer now has the choice of whether to manually manage memory of objects or not. Opinions on this are divided. Some say that reference counting is superior because it allows the programmer to have precise control over when objects are deallocated, but does not add the burden of doing so for every object a program allocates, nor incur the performance penalty usually associated with automatic garbage collection. Others say the entire scheme is unnecessary, that Java-style automatic garbage collection is superior, because it significantly reduces the possibility of programmer error in memory management. Cocoa garbage collection is fully backwards compatible; it is only used if the project settings are modified to take advantage of it. Furthermore, garbage collection can be turned on or off at different points in the program.
Cocoa consists primarily of two Objective-C object libraries called frameworks. Frameworks are functionally similar to shared libraries, a compiled object that can be dynamically loaded into a program's address space at runtime, but frameworks add associated resources, header files, and documentation.
A key part of the Cocoa architecture is its comprehensive views model. This is organized 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.
The Smalltalk teams at Xerox PARC eventually settled on a design philosophy that led to easy development and high code reuse. Known as "model-view-controller" (MVC), the concept breaks an application into three sets of interacting object classes. Model classes represent raw data, such as documents, settings files, or objects in memory. Views are, as the name implies, representations (often visual) of the data. Controller classes contain logic which links the models to their views, and maintains state to keep them synchronized.
Cocoa's design is a strict application of MVC principles. Under OpenStep, most of the classes provided were either high-level View classes (in AppKit) or one of a number of relatively low-level model classes like NSString. Compared to similar MVC systems, OpenStep lacked a strong model layer. There was no stock class which represented a "document," for instance. During the transition to Cocoa, the model layer was expanded greatly, introducing a number of pre-rolled classes to provide functionality common to desktop applications.
In Mac OS X 10.3, Apple introduced the NSController family of classes, which provide predefined behavior for the controller layer. These classes are considered part of the Cocoa Bindings system, which also makes extensive use of protocols such as Key-Value Observing and Key-Value Binding. The term 'binding' refers to a relationship between two objects, often between a view and a controller. Bindings allow the developer to focus more on declarative relationships rather than orchestrating fine-grained behavior.
With the arrival of Mac OS X 10.4, Apple extended this foundation further by introducing the Core Data framework, which standardizes change tracking and persistence in the model layer. In effect, the framework greatly simplifies the process of making changes to application data, undoing changes (if necessary), saving data to disk, and reading it back in.
By providing framework support for all three MVC layers, Apple's goal is to reduce the amount of boilerplate or "glue" code that developers have to write, freeing up resources to spend time on application-specific features.
Under Objective-C, methods are represented by a selector, a string describing the method to be called. When a message is sent, the selector is sent into the ObjC runtime, matched against a list of available methods, and the method's implementation is called. Since the selector is text data, this allows it to be saved to a file, transmitted over a network or between processes, or manipulated in other ways. The implementation of the method is looked up at runtime, not compile time. There is a small performance penalty for this, but late binding allows the same selector to reference different implementations.
By a similar token, Cocoa provides a pervasive data manipulation technique called key-value coding (KVC). This permits a piece of data or property of an object to be looked up or changed at runtime by name — the property name acts as a key to the value itself. In traditional languages, this late binding is not possible. KVC leads to great design flexibility — an object's type does not need to be known, yet any property of that object can be discovered using KVC. In addition, by extending this system using something Cocoa calls Key-Value Observing (KVO), automatic support for Undo/Redo is provided.
One of the most useful features of Cocoa is the powerful "base objects" the system supplies. As an example, consider the Foundation classes
NSAttributedString, which provide Unicode strings, and the
NSText system in AppKit, which allows the programmer to place string objects in the GUI.
NSText and its related classes are used to display and edit strings. The collection of objects involved permit an application to implement anything from a simple single-line text entry field to a complete multi-page, multi-column text layout schema, with full professional typography features such as kerning, ligatures, running text around arbitrary shapes, rotation, full Unicode support and anti-aliased glyph rendering. Paragraph layout can be controlled automatically or by the user, using a built-in "ruler" object that can be attached to any text view. Spell checking is automatic, using a single dictionary used by all applications that uses the "squiggly underlining" introduced by Microsoft (actually a dashed red underline in Cocoa). Unlimited Undo/Redo support is built in. Using only the built-in features, one can write a text editor application in as few as 0 lines of code. With new controller objects, this may fall to zero. This is in contrast to the TextEdit APIs found in the earlier Mac OS.
When extensions are needed, Cocoa's use of Objective-C makes this a straightforward task. Objective-C includes the concept of " categories" which allows for modifications to an existing class "in-place". Functionality can be accomplished in a category without any changes to the original classes in the framework, or even access to its source. Under more common frameworks this same task would require the programmer to make a new subclass supporting the additional features, and then change all instances of the classes to this new class.
The Cocoa frameworks are written in Objective-C, and hence Objective-C is the preferred language for development of Cocoa applications. Java bindings for the Cocoa frameworks (known as the "Java bridge") are also available but have not proven popular amongst Cocoa developers. Further, the need for runtime binding means many of Cocoa's key features are not available with Java. In 2005, Apple announced that the Java bridge was to be deprecated, meaning that features added to Cocoa in Mac OS X versions later than 10.4 would not be added to the Cocoa-Java programming interface.
AppleScript Studio, part of Apple's Xcode Tools makes it possible to write (less complex) Cocoa applications using AppleScript. There is also a third-party scripting language specifically designed for Cocoa, called F-Script, that allows for native access to Cocoa objects and provides a suite of graphical introspection tools.
Third party bindings are also available for other languages:
A more extensive list of implementations is available.
There are also open source implementations of major parts of the Cocoa framework that allows cross-platform (including MS Windows) Cocoa application development:
API SUES MOTOR OIL MARKETERS OVER TRADEMARKS USAGE THE AMERICAN PETROLEUM INSTITUTE IS ACCUSING TWO COMPANIES OF SELLING ENGINE OILS THAT BEAR API'S TRADEMARK WITHOUT ITS AUTHORIZATION.
Nov 14, 2011; WASHINGTON -- The following information was released by the National Association of Convenience Stores: The American Petroleum...
API Fields Mighty Linux-on-Alpha 1U.(marketing firm formed by Compaq and Samsung changes name)(from Alpha Processor Inc to API NetWorks, will)(develop dedicated rackmount servers)(Company Business and Marketing)
Nov 06, 2000; Alpha Processor Inc (API), the marketing operation that Samsung Electronics and Compaq put together to peddle the Alpha...
API wants compromise with MVMA trade group. (American Petroleum Institute; Motor Vehicle Manufacturers Association of the United States; Automotive Lubes)
Oct 23, 1990; API Wants Compromise With MVMA Trade Group Despite the announcement late Monday of a new oil specification for North...