Cocoa (API)

From Academic Kids

Missing image
Cocoa_dev_screenshot.png
A Cocoa application being developed using Xcode. Interface Builder windows are also visible.

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. As such, Cocoa applications are generally characterised by sleek, clean interfaces, and good performance.

Contents

Cocoa history

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 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. The program was discontinued in one of the rationalizations that followed Steve Jobs' return to the company.

Memory management

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 most classes, both vendor and user, are derived, implements a reference counting scheme for memory management. Objects derived from the NSObject root class respond to a retain and a release messages, and keeps a retain count, which can be queried by sending a retainCount message. 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 the end of the program's event loop, guaranteeing program flow has passed out of the block where that object was 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.

Cocoa gives the programmer the choice of whether to manually manage memory 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, nor incurs the performance penalty that usually goes with automatic garbage collection. Others say that the entire scheme is unnecessary, and that Java-style automatic garbage collection is superior, because it removes the possibility of programmer error in memory management.

A combination of the two features is also possible. Modern garbage collectors often include features to be started and stopped mid-task, allowing the application to control how much time will be taken up whenever the system is called. Combining such a system with AppKit's "do it in the event loop" appears to offer a best-of-both-worlds solution. Such a system was successfully implemented under GNUStep, GNU's open source analog of OpenStep.

Main frameworks

Cocoa consists primarily of two Objective-C object libraries called frameworks. Frameworks are a construct unique to the NeXTSTEP/OpenStep/Cocoa family of programming environments. They are functionally similar to shared libraries, a compiled object that can be dynamically loaded into a program's address space at run-time, but frameworks add associated resources, header files, and documentation. Cocoa also includes a powerful versioning system to prevent the sort of problems that occur under Microsoft Windows, DLL Hell.

  • Application Kit or AppKit is directly descended from the original NeXTSTEP Application Kit. It contains code with which programs can create and interact with graphical user interfaces. NSWindow and NSButton are examples of AppKit classes.
  • Foundation Kit, or more commonly simply Foundation, first appeared in OpenStep. Foundation is a generic object-oriented library providing string and value manipulation, containers and iteration, distributed computing, event handling, and other functions that are not directly tied to the graphical user interface. NSString, NSDictionary and NSURLHandle are examples of Foundation classes. The "NS" prefix, used for all framework objects, comes from Cocoa's NeXTSTEP/OpenStep heritage.

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.

Model, View, Controller

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, or MVC, the concept breaks an application into three sets of interacting object classes. Cocoa's design is a strict application of MVC principles.

In more recent versions of Cocoa, those shipping with OS X 10.3 and later, Apple has started to provide pre-rolled controller objects. Apple uses the term "bindings" to refer to these controllers, since they "bind" data in the model to controls and other elements in the user interface.

In Mac OS 10.4, Cocoa provides automatic support for the data model too, called Core data. By providing framework support for all three MVC layers, developers are freed from writing much boilerplate-type code, and can spend time on more worthwhile areas, such as the features that make their application unique.

Invocations and bindings

In most object oriented languages, calls to methods are represented physically by a pointer to the code in memory. This constrains the design of an application since specific "command handling" classes are required, usually organised according to the chain of command design pattern. While Cocoa retains this approach for the most part, the late binding possible with Objective-C opens up more flexible possibilities.

Under Objective-C calls can be represented instead by an invocation, essentially a string describing the call to be made. When a call is made, the invocation is sent into the ObjC runtime, matched against a list of possible methods, and then called. Since the invocation is text data, this allows it to be saved to a file, transmitted over a network, or manipulated in other ways. The "binding" between the call and the function is deferred until runtime, rather than needing to be resolved at compile time. There is a performance penalty for this, but this is small and outweighed by the advantages. The Cocoa GUI builder, Interface Builder (IB), makes extensive use of this facility.

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, and it leads to greate 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 called Key-Value Observing (KVO), automatic support for Undo/Redo is easily provided.

Rich objects

One of the most useful features of Cocoa are the powerful "base objects" the system supplies.

As an example, consider the Foundation class NSString, and the NSText system that manipulates it in the GUI. As well as providing storage for unicode character data, it provides many methods for doing complex manipulations of strings.

NSText and its related classes is the objects used to display and edit strings. The collection of objects involved permit the same code 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, and of course 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 view. Spell checking is automatic, using a single universal dictionary used by all applications that uses the "squiggly underlining" introduced by Microsoft. Unlimited Undo/Redo support is built in. Using only the built-in features, one can write a text editor application in as few as 13 lines of code. With new controller objects this may fall to zero. This is in contrast to the TextEdit APIs found in the earlier MacOS.

When extensions are needed, Cocoa's use of Objective-C makes this a straightforward task. ObjC includes the concept of "categories" which allow code to be added onto existing classes at runtime. Adding 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.

Implementations

The Cocoa frameworks are written in Objective-C, and hence Objective-C is presently the preferred language for the Cocoa applications. Java bindings for the Cocoa frameworks are also available, but do not yet appear to have seen much real-world use. In addition, the necessity of run-time binding means that many of Cocoa's key features are not available when using Java. AppleScript Studio, part of Apple's Xcode Tools makes it possible to write (less complex) Cocoa applications using AppleScript.

Third party bindings are also available for other languages:

A more extensive list of implementations (http://www.fscript.org/links.htm) is available.


There is also an open source implementation of major parts of the Cocoa framework that allows cross-platform (including MS Windows) Cocoa application development. It is called GNUstep.

External links

de:Cocoa es:Cocoa fr:Cocoa it:Cocoa ja:Cocoa

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools