Hot Chocolate!

By John Hörnkvist

An Introduction to Cocoa

Introduction

Mac OS X is Apple's great gamble for the future. It does away with almost two decades worth of heritage, and brings advances such as preemptive multitasking, virtual memory and memory protection to the Macintosh. The transition, however, requires that developers port their applications to one of two new APIs; Carbon or Cocoa. While Carbon is clearly the basic element in Apple's plan for bringing existing Mac OS applications to the new operating system, Cocoa offers great opportunities for developers that bring new applications to the market, and to those that are to rewrite from a clean slate. In this article, I will describe what Cocoa is, and how you can take advantage of it.

Apple has repeatedly pointed out that you can write Cocoa applications in either Objective-C or Java. However, Cocoa itself is an Objective-C system, and you'll probably enjoy more of its benefits if you use Objective-C yourself. Therefore, all code examples in this article will be in Objective-C.

Heritage

Cocoa is the latest name for the object oriented software frameworks and technoligies Apple received in the NeXT takeover. In previous incarnations, the technology has been know as YellowBox, OpenStep and NEXTSTEP - and until now, the largest step by far happened in the transition from NEXTSTEP to OpenStep.

The greatest difference between Cocoa and its predecessor is the replacement of Display Postscript by Quartz. While DPS was a very powerful graphics engine, it was a client server system, and an interpreted language, which combined to make life difficult for the performance oriented developer - it was possible to get excellent performance out of DPS, but it required an intimate understanding of the graphics pipeline[1]. With Quartz, graphics display has been moved to a shared memory model, making graphics programming as convenient as the rest of Cocoa - at the cost of making remote display "a third party opportunity".

Cocoa has also gained many important components since Apple took over, such as the document architecture and scriptability.

Cocoa is the latest name for the object oriented software frameworks and technoligies Apple received in the NeXT takeover. In previous incarnations, the technology has been know as YellowBox, OpenStep and NEXTSTEP - and until now, the largest step by far happened in the transition from NEXTSTEP to OpenStep.

The greatest difference between Cocoa and its predecessor is the replacement of Display Postscript by Quartz. While DPS was a very powerful graphics engine, it was a client server system, and an interpreted language, which combined to make life difficult for the performance oriented developer - it was possible to get excellent performance out of DPS, but it required an intimate understanding of the graphics pipeline[1]. With Quartz, graphics display has been moved to a shared memory model, making graphics programming as convenient as the rest of Cocoa - at the cost of making remote display "a third party opportunity".

Cocoa has also gained many important components since Apple took over, such as the document architecture and scriptability.

The Objective-C Language

Objective-C is the brain child of Brad J. Cox, and the result of his worries about the difference in productivity between the hardware and software industries. In the early eighties, he noticed that software development productivity was on a worrying growth curve, and predicted a software crisis if development methodologies did not change. Dr. Cox attributed the difference between hardware and software industries to the fact that the hardware industry used components, such as integrated circuits, ICs, in their designs, and created new components only when necessary. The software industry, at the time using C or Pascal, lacked the component based approach to development, and Dr. Cox set out to change that.

The result of Cox's work was Objective-C, a dynamic object oriented language in the Smalltalk tradition, but built on C. Objective-C allows you to use "software ICs", using Cox's term, to do component based engineering, and it allows you to create new components all the way down to the "transistor level" since it is built on C. Objective-C allows you to drop back to C, but trying to write the program as much in the "Objective" part as possible is a better approach, much like hardware engineers should avoid implementing their designs at the transistor level - unless there is a compelling reason, such as need for extreme performance, that validates the a level approach.

Objects, Instances, Classes, Methods and Messages

Many of the terms used in the article will already be familiar to most readers. Some would claim that the terms are familiar to all; quoting Dr Cox:

"Betsy is a cow" is just a short form of "Betsy is an instance of class cow."

What may be new to some is the context where these terms are used.

An object is a set of private data and some operations, methods, that can access and operate on that data. It is also an instance of a class.

Data is contained within objects as instance variable. In some language, you can also store data in the classes themselves. There is no support for class variables in Objective-C.

A message is a request sent to an object in order for it to perform a method.

A class is a classification of an object, as well as a concrete entity in Objective-C. You can instruct a class to create an instance of itself, and all objects know what class they belong to. In fact, classes are objects themselves.

Since you can send messages to both instances and classes, it is sometimes useful to make a distinction between instance methods and class methods.

A framework is a dynamically loadable library of classes, categories, protocols, functions, and everything else you'd expect from a linked library, optionally bundled with header files, documentation and other resources.

Syntax

Objective-C extends the C syntax in some simple but very powerful ways. Most important is the overloading of brackets as a shorthand for message sending.

Sending a message

There are two ways of sending messages; a cumbersome way using traditional C syntax, and a simple way using the Objective-C syntax.

The name of the class is KitchenTool, its super class is NSObject, it has an instance variable called name, you create it by calling the class method newKitchenToolWithName: (class methods have a plus sign rather than a minus sign before their declaration) and you can retrieve a tool's name by calling the name method.

You also create an implementation of the object, containing the definition of its methods. An implementation of KitchenTool would look like this:

Take note of the dealloc method; every class that has objects as instance variables should have a dealloc method which releases instance variables during deallocation. The dealloc method sends a message to super, which means that the implementation of dealloc in NSObject, KitchenTool's super class, also gets called.

You may also note that in the newKitchenToolWithName: method, the class object accesses the object as a structure. This is allowed because the class methods are part of the implementation of the class, but I do not recommmed using this feature except in creators for immutable classes.

The use of an #import directive rather than #include avoids loading the same header file multiple times.

Self and Super

When you send a message to an object, you pass the object a pointer to itself, which is called self. The existance of self lets the object use method passing and send messages to itself. Avoiding direct access to instance variables, except in direct accessor methods, and splitting large methods into many smaller ones increases the power of inheritance, and is highly recommended.

Sometimes, you need to access a super class' definition of a method, even though you've overridden it. To send a message that invokes the implementation in the superclass, you use super instead of self when you send the message.

Dynamic features of Objective-C

The use of message passing instead of function calls in Objective-C has some interesting consequences. Among other things, it allows posing, forwarding, categories and swizzling.

Another effect of dynamic message passing is how overriden methods are handled; if you have a class that implements

- (void) someMethod
{
[self someMethodWithNumber:5];
}
- (void) someMethodWithNumber:(int)aNumber
{
// Do something with a number
}
and a subclass that overrides this with
- (void) someMethodWithNumber:(int)aNumber
{
// Do something different with a number
}

then, when someMethod is called on an instance of the subclass, someMethod will use the subclass's implementation of someMethodWithNumber: rather than the implementation in the class where someMethod was defined.

Posing lets a class take the place of its parent in the object hierarchy. The parent class still exists, so the posing object can still callt super, but instances of the parent class will never be created; instead, instances of the poser will be created. A posing class cannot declare new instance variables.

Forwarding is used as a fault handler; when an object receives a message for a method that it doesn't implement, forwarding gives it a chance to pass the message on to another object, or provide some other error handling.

Categories allow you to override or add methods to an existing class, without having to create a subclass or access the source.

Swizzling lets you change the class of an object at any time. This is a dangerous thing to do, but it can be very useful.

To find out more about Objective-C, I suggest reading Object-Oriented Programming and the Objective-C Language, which is available at

Frameworks

There are three major parts of Cocoa; Foundation Kit (Foundation), Application Kit (AppKit) and Enterprise Objects (EOF). Foundation is the lowest layer of the system, classes that can be used whether you're building a program without any user interface, or an application with a graphical user interface. The AppKit is built on top of Foundation, and provides graphical functions, as well as high level objects that simplify the construction of applications. Enterprise Objects is a set of frameworks, some built on Foundation and some on AppKit, that provide astoundingly easy and powerful access to databases.

Foundation Kit

The role of the Foundation Kit is to shield the programmer from the underlying operating system by providing calls for memory allocation and deallocation, file access, shared memory, thread management, task management, inter process communication and so forth. It also provides some basic utility classes, such as strings, arrays and dictionaries.

One of the most common mistakes of fledgling Cocoa programmers is to underestimate the richness and flexibility of the Foundation classes. Take a good look at the Foundation class hierarchy before you start writing any new code! On the other hand, a common mistake of more experienced Cocoa programmers is to overuse the Foundation classes. When you know something about your data that can help you make more efficient use of memory and processor power than what Foundation provides, then you should use that knowledge. But make sure that Foundation's classes don't suffice first.

If, for example, you have data that you need to be indexed by date, you could either put it in an NSDictionary, or, you could make your own specialized class for that data. The specialized class is easy to write, and provides better performance. But, writing it creates means having an additional class to test and debug.

Mutable and Immutable objects

In the Foundation Kit, you will find classes named NSArray and NSMutableArray, NSString and NSMutableString and so forth. An immutable object, such as NSArray and NSString, cannot be changed; its contents are set at the time of creation. A mutable object, on the other hand, can have its contents changed at any time. This difference is very useful, since it allows the developer to make some optimizations. For example, if an object contains an NSArray, it can safely pass that array to another object without risking that the array changes, even though the objects in the array can still change internally. If the object uses an NSMutableArray, it is prudent to return a copy of the array.

One should also consider the possibility of mutability when an object is returned from a method. Even if the interface specifies that a method returns an immutable object, the receiver should not rely on the immutability of it; since the mutable class is often a subclass of the immutable class, the compiler will accept an NSMutableArray instead of an NSArray, for example. This also affects thread safety of the classes. While immutable objects are generally safe to use in a multi threaded application, mutable objects need special care. An example of an unsafe implementation is show in listing 1. Further discussion on multithreading in Cocoa is outside the scope of this article, and in fact, Apple has not yet made public what classes will be thread safe in Mac OS X, although it should be safe to assume that classes that are thread safe in earlier version of the framework will remain safe.

Enumerators are objects that represent iteration over a collection of objects, such as an array or a dictionary. Enumerators provide a consistent interface to several kinds of collections, and using them gives code that is easier to read and to change. It also protects you from worrying about changes in the collection you are iterating over; you entrust that to the enumerator.

The drawback of enumerators is a slight performance cost, and sometimes delayed destruction of objects.

Class clusters

Class clusters are used extensively in the Foundation Kit. Their role is to give a common interface to a set of anonymous classes. There is a class called NSArray in Foundation, but no NSArray objects are created when a program runs. Instead, when you try to allocate an NSArray, you're given an instance of one of NSArray's subclasses. Other class clusters include NSDate and NSString. For each class cluster, there is a minimal set of methods that a subclass has to implement in order to work as a member of the cluster.

When an object is the root of a class cluster, you can't easily subclass it to add functionality. While this may seem limiting at first glance, it is very easy to work around. Instead of writing a subclass, you can write a category on the cluster's root class which extends its behavior, or you can use composition to create a class that combines the capabilities of several classes.

If for example, you wish to create an object that is both an array and a dictionary, you should not do it by creating a subclass of NSArray and implementing dictionary methods on that. Instead, create a new class that composes the functionality of an array and a dictionary.

In fact composition is a very powerful technique, and by using it wisely you'll find that your programs become both smaller and more flexible. Composition is described in detail later in this article.

Object ownership

An important part of Foundation is memory management. Cocoa uses a system known as manual reference counting. When the reference count of an object reaches zero, it is deallocated.

Listing 3 shows how an object can handle retain and release. Note that when an object is allocated, all its instance variables are set to nil. Since Objective-C disregards message passing to nil, it is safe to send messages to uninitialized objects.

The rules of retain and release are relatively simple, but nonetheless, it is an area where programmers tend to err. For example, the unsafe method in listing 4 shows a common mistake among unexperienced Cocoa programmers; it is easy to forget that while the conventions for object ownership state that unless you get an object by calling alloc/init or copy, you don't have to release it, it is not guaranteed that the object will stay alive unless you retain it. Learn the rules by heart, because retain/release bugs are among the most difficult to track down.

Listing 4: Safe and unsafe usage of retain and release. The final version using autorelease avoids a memory leak if an exception is raised before release. This safety comes at a performance and memory cost.

A highly useful part of Foundation is Distributed Objects. Distributed Objects gives objects in different tasks and threads a convenient way of communication.

The most important parts of Distributed Objects are NSConnection, which encapsulates the communication channel, and NSDistantObject, which acts as a stand in, or a proxy, for an object that lives on the other side of a connection, forwarding messages and handling returned values. A connection can be named, which makes it available to other applications on the systen, or anonymous, which makes it useful for private channels, such as between threads in an application. Communication with distibuted objects is thread safe. Distributed Objects also works over networks, but the semantics for this have changed because of the kernel change in Mac OS X, and it is not yet clear how named object services will work.

Creating and connecting to a connection is very easy, as you can see from listing 5.

Listing 5: Vending an object, and connecting to an object using Distributed Objects

The OmniGroup has a set of highly useful foundation level frameworks that facilitates networking, among other things. You'll find more information about the frameworks and the license at <http://www.omnigroup.com>.

MPWFoundation from Marcel Weiher of Metaobject GmbH is particularly interesting; it offers some useful extensions for higher order programming. For example, the code in listing 2 can be reduced to:

The MiscKit (<http://www.misckit.com>), a community project managed by Don Yacktman, includes MiscFoundation, which extends the Foundation framework. The MiscKit was a useful resource on NEXTSTEP, but has remained in early beta state on OpenStep/Cocoa for several years. The MiscKit is governed by the MiscKit license.

Sen:te has created OCUnit, and Objective-C version of the Kent Beck's Smalltalk testing framework. OCUnit is highly recommended for developers that value integrated testing support. You can find OCUnit at <http://www.sente.ch/software/ocunit>.

A framework called ConcreteProtocol, that exploits the Objective-C runtime to give a kind of multiple inheritance based on protocols, and a kit for software licensing, LicensingKit can be downloaded from ToastedMarshmallow.com at <http://www.toastedmarshmallow.com>. The LicensingKit is mentionware, and the ConcreteProtocol framework is mentionware for freeware developers, and commercial for commercial developers.

There is also a commercial framework for software licensing called the Licenser Kit from Stone Design (<http://www.stone.com>) and Caffeine software.

Application Kit

The AppKit is the part of Cocoa that is responsible for the graphical user interface. Founded on the dynamic aspect of Objective-C, the kit greatly simplifies the task of creating a user interface. In the AppKit you'll find support for drawing, document based applications, a large collection of useful interface components. The Application kit contains over 100 classes and protocols, which makes it impossible to describe it in its entirety here. Instead some of the more interesting classes will be described.

NSView and NSCell

Whenever there is drawing, there is an NSView. Every window contains at least one view. A view can have a number of subviews, each which can have subviews in its turn.

Views are also where user interaction takes place. A mouse click in a window that isn't directly handled by the window itself will get sent to the highest level view in the window at the point of the click, as a mouseDown: message.

NSTextView is a very useful class for displaying text. It has support for fonts and pictures, as well a fair amount of layout support. While it may not suffice for a word processor, it is usually more than enough for text entry in other applications.

An NSCell can be regarded as a lightweight view class, but in fact it is an object that you draw in a view. Unlike a view, a cell does not have a coordinate system of its own, or any knowledge of its position. The cell is just a representation of some data that can be drawn. The cells have a number of advantages over views; it is faster to draw many cells in a single view than to draw many views, cells need less memory and they can more readily be reused. The drawback is that it is more difficult to set up drawing of a cell. Since the cell doesn't know where to draw itself, or in what coordinate system, you have to set everything up yourself.

One useful cell class is NSImageCell, which composites an image to draw itself.

Mostly, cells are used with existing classes with composition. For example, NSMatrix displays a set of NSCells in a matrix, and NSTableView displays a set of cells as a table. Using your own cell classes with these is trivial.

NSImage and NSImageRep

NSImage and NSImageRep provide a powerful system for compositing and drawing. Before Quartz, one of the image representations was NSEPSImageRep, which allowed you to draw EPS (Encapsulated Postscript) into a view. With Quartz, one may expect an NSPDFImageRep instead. Ine essence, all you need to do to add support for a new image format in Cocoa is to create the proper subclass of NSImageRep; the display system in NSImage is responsible for selecting the proper representation for each image.

The NSCustomImageRep class is interesting, because instead of knowing how to draw itself, it calls a delegate object which does the drawing. By using this class, you can easily make images draw just about any data, and by combining this with NSImageCell, you can get custom drawing in any object that supports cells.

NSImage is a container for image representations. To increase performance, NSImage will cache a representation, so the actual drawing code is only executed the first time the image is composited. However, an NSImage will use a cache only if it fits the resolution and colorspace of the view where it is drawn. This ensures that printed graphics get the highest resolution possible.

NSBezierPath and NSAffineTransform

NSBezierPath is the class for advanced drawing in Cocoa. The advantages of using NSBezierPath instead of discrete drawing commands are that you can create the NSBezierPath once and then draw it over and over, which cleans up your code, and that you can apply transforms to a bezier path before drawing it. While you can apply a transform to the view where the graphic is drawn to achieve the same effect with discrete commands, this is more costly if the graphics needs to be redrawn, since the calculations involved in the transformation have to be done each time the graphic is drawn. With NSBezierPath, the transformation is applied only once, since it affects the drawing commands in the bezier path rather than the canvas.

NSAffineTransform is the transform class of Cocoa. The fact that the transform is affine means that lines that are parallel before the transform are parallel after the transform also. Thus, it does not attempt to do three dimensional transforms such as perspective. However, this is a limitation of Quartz, not of Cocoa; you can create your own transform classes and apply them to NSBezierPaths.

Extensions to the AppKit

The Omni Group has some application level frameworks, some of which are very rich.

Finally, the MiscKit's application framework includes several useful components.

Enterprise Objects Framework

Enterprise Objects is a set of frameworks for database access and display of database data. EOF insulates from the particulars of databases; the same program can be used with any database for which you have a database adapter. Thus, a developer can write software that is essentially database independent. EOF uses the Entity-Association (also known as Entity-Relation) model of databases, which makes database modeling very convenient, but maps this onto a relational database model, allowing the use of relational database servers, such as Oracle and Sybase. Since EOF maps to a relational model, you can build applications for existing databases without having to convert the old data to a new format.

Enterprise Objects can be used in server programs, as well as in applications. Whenever you need persistent storage of large amounts of data, EOF is an excellent candidate.

Cocoa Programming Techniques

In Cocoa, you'll find that you can increase your productivity significantly by not programming; with procedural APIs, you'll spend 20% of your time searching libraries, and 80% typing and debugging code. In Cocoa, you spend 20% of your time programming and debugging, 30% searching the frameworks, and 50% enjoying your newly found spare time.

However, sometimes you really must write some new code, and for those occassions, I'd like to point to a number of programming techniques that are highly efficient in Objective-C and Cocoa. Some of these methods should be well known to programmers skilled in C++, but Objective-C has a dynamic dimension which C++ lacks, which affects the choice of design patterns.

Inheritance

Inheritance is the abused workhorse of object oriented programming. If you look at the Cocoa APIs, you'll find that inheritance plays a significant role in the design of the AppKit, but is used sparingly in Foundation. This is done because inheritance creates dependencies between objects, breaking the encapsulation. When inheritance is used, changes to a class may break its subclasses. Thus, use inheritance when the kits point you to that method; NSView is an example of this. Don't use it where it is clear that Apple wants to hide the details, as with the class clusters.

Posing

Posing is useful when you need to refine the behavior of an object, and cannot use a category. It is a rather crude method, and in my opinion should only be resorted to for debugging, and when no other method suffices.

Categories

Categories allow you to add methods to an existing class without subclassing. The advantage of this is that it allows modularity of design - different parts of an object's functionality can be kept in different files and frameworks - and that extensions to a class becomes ubiquitous. Every instance of the class will have the new methods, as will every instance of its sub classes.

Categories override existing methods - unless the developer takes pains to avoid it. This has the advantage that you can replace malfunctioning methods in existing classes without having access to the source. It is also a danger, because you can accidentally override a method - a problem which is accentuated by the lack of name spaces in Objective-C.

Protocols

Protocols define interfaces without specifying an implementation. This allows the compiler and runtime to do type checking based on the interface rather than on the class hierarchy. Protocols are useful when there is functionality that can be implemented in many completely independent ways, and in some cases where multiple inheritance would otherwise be needed.

Whenever an object uses a delegate, it is wise to have a protocol that defines what delegates must be able to do. This allows the compiler to warn the developer if the wrong object is used as a delegate, and it allows the developer to insert runtime checks of protocol conformance.

Protocols are also very useful with Distributed Objects, since you can use protocols to limit the capabilities that an object advertises over a connection, and also to do validity checking on the sending side rather than on the receiving side, which decreases communications and increases performance.

Composition, forwarding and delegates

Composition means combining the capabilities of many classes into one by creating an object that contains several other objects and delegates parts of its responsibility to them. Composition can be used as a replacement for multiple inheritance, but is far more general than that. The great advantage of composition over inheritance is that it can provide shorter and more general code. A simple example is glyphs; instead of creating a subclass for each possible glyph, you create a general glyph class, and use composition to get the attributes you want. I.e. you don't create a TimesRomanGlyph, TimesRomanBoldGlyph, HelveticaGlyph and so forth, but have a character glyph class on which you composite font, color and any other attributes. To generalize further, you may want to create an GlyphAttribute class or protocol, such that Font conforms to the GlyphAttribute protocol, or GlyphFont is a subclass of GlyphAttribute. By doing this carefully, a CharacterGlyph simply becomes a character and a list of attributes. The CharacterGlyph itself has no need to know the particulars of the attributes, since they provide a common interface.

Some advocates of composition claims that it should replace inheritance completely, since it avoids problems such as fragile base classes, and is more flexible. The drawback of composition is that it creates an additional layer of indirection, which decreases performance.

To facilitate composition, NSObject provides a method called forwardInvocation:. When you send an object a message that it doesn't implement, instead of causing an error, forwardInvocation: will be called, giving you a chance to pass the message on to another object.

Delegates are used in a special form of composition. An object hands over a part of its functionality to a delegate object, allowing customization without subclassing. A text object may let the delegate inspect the incoming text and do completion or spell checking on it, for example. An object can also let its delegate observe and decide over certain behaviours. As an example of this pattern, NSWindow uses a delegate with which it confirms that it is OK to perform certain actions, such as closing.

Observers and Notifications

An observer is an object that tracks changes to other objects. Since an object is not allowed to look at the internals of other objects, this must be implemented by having the observed object inform its observer of changes through notifications. However, this would mean that the observed object would have to be aware of all observers, creating unnecessary interdependence between object designs. To avoid this, an notification center acts as a broadcasting station for notifications. The use of a notification center means that the notifier no longer has to be aware of its observers. This is highly useful, because it decouples the observed object from the observer, allowing many different observers to monitor an object without any changes to the object itself. Encapsulation is maintained, since it is the object that decides when to send notifications, although it has no control over who will receive it.

The Foundation Kit provides a notification broadcasting facility, NSNotificationCenter, to which you can send notification objects, NSNotification instances. When a notification center receives a notification, it inspects it, and send it to all objects that have registered as observers for this kind of notification.

Many classes in the Application kit utilize notifications. For example, an NSWindow will send notifications when its status changes, such as when it is moved, sent to another screen, becomes the main window, and so forth. By observing such changes, other parts of the user interface can properly reflect the status of windows.

The difference between delegates and observers is that a delegate is much closer to the observed object; the object knows about its delegate and actively delegates responsibility to it.

Singletons

Singletons are classes that only have one instance. When you try to allocate more objects of such a class, you get the same object back. This is useful when you allocate an immutable object with the same data repeatedly. For example, in a word processor where you represent each glyph with an object, you're likely to have a lot of instances of the letter 'e' in your default font. Then, instead having one copy of the 'e' glyph for each time it appears in the text, you have multiple references to the same glyph instance, saving an immense amount of memory. Of course, this means that the glyph cannot have a notion of position, but that is probably a good design choice in a word processor anyway.

The Model View Controller paradigm

The Model View Controller, or MVC, paradigm has been in the toolbox of object oriented programmers for over two decades. The central idea of this paradigm is breaking an application, or subsystem into three parts; the model, which represents the data in your application, the view, which is the visualization of that data, and the controller, which manages the other objects, such as by taking user input on the view and translating that to changes in the model.

The MVC paradigm is a high level design pattern, and you may have many classes in each category in an application.

The great advantage of the MVC paradigm is that it separates the data storage code from the display code and the user interface code, making it relatively easy to create a new user interface for an application. Thus, you can create a web based interface for a database application, while sharing the database code with the Aqua interface, for example.

The Cocoa application kit is extrememly suitable for MVC programming, and the document support in Cocoa follows the MVC paradigm. There is an Apple article called "Application Design for Scripting, Documents, and Undo" that explains this concept further. [<http://developer.apple.com/techpubs/macosx/>]

Development tools

Cocoa development is centered around ProjectBuilder, an application which lets you edit and create files, which manages the compiler, and interacts with InterfaceBuilder to let you create user interfaces for your applications. InterfaceBuilder is a fully object oriented user interface design application built on a palette model of components.

Another part of the tool suite is EOModeler, which lets you create Enterprise Objects data base models. You can drag database objects from EOModeler to InterfaceBuilder to quickly create user interfaces for database applications.

ProjectBuilder

ProjectBuilder has always been a relatively simple integrated development environment, and a fair amount of complaints about it has been heard from Macintosh developers that are converting to OPENSTEP. NeXT was a small company and didn't have the resources to perfect every part of the developer environment, but with Apple's significantly greater resources, the rough edges of ProjectBuilder should be polished off before the release of Mac OS X.

Figure 1. A fresh framework project in ProjectBuilder

ProjectBuilder utilizes adapted versions common tools to do much of its job; it uses the Gnu C Compiler (gcc), gnumake and the Gnu debugger (gdb). The use of these tools is hidden to a varying degree; gcc and gnumake live quietly in the background, while the user has to interact directly with the debugger to use the full power of gdb.

Despite its faults ProjectBuilder does an acceptable job even in its pre-X state, and its indexing and formatting functions for Objective-C code are quite good. Extensions to ProjectBuilder provide word completion and other features.

InterfaceBuilder

When I first tried to learn NextStep development back in 1994, InterfaceBuilder was by far the greatest hurdle I ran into; the conceptual step from traditional procedural user interface development to InterfaceBuilder's pure object model was simply too great. It took over a month before I figured out that you couldn't understand how NextStep programs work by looking at the code, you had to look at the connections between objects, and that those connection lived in the interface binaries.

In InterfaceBuilder, you create user interfaces by dragging and dropping components from palettes, by instantiating control and model objects, by creating connections between objects and by the setting objects' instance variables. Figure 2 shows how the target of a button titled "Done" is connected to the licenseDone: method of the object called "AppDelegate". (Naming the objects is not necessary, InterfaceBuilder will create a default name based on the object's class, but it is a convenient way of documenting the interface.)

Figure 2. Connecting a button to an action in Interface Builder

Unlike many GUI builders, InterfaceBuilder does not create code, instead it creates an interface description, in Mac OS X this will be saved as an XML file. The interface description is a complete picture of the state of the objects in the user interface - sometimes refered to as freeze dried objects.

Component palettes

With a small effort, any user interface object can be turned into a palette, but for most applications, the palettes that come with InterfaceBuilder are sufficient.

Once you have an object as a component of a palette, you don't need to create a new palette for subclasses; by using the "custom class" inspector, you can tell interface builder to use any subclass of the palette component. This is property is set for each instance, rather that for each class, which means that you're can use a separate custom class for each button in your interface, should the need arise. (If it does arise, you're likely to be better off by using composition than by subclassing, but InterfaceBuilder doesn't force you to follow that approach.)

Apple is likely to refine InterfaceBuilder before the final release, but the palette organization from OPENSTEP 4.2 gives at least an idea of what we can expect.

The menu palette gives you easy access to the default Cocoa menu structure. By simply dragging the document menu onto the menu bar, you get the exact menu structure that Apple's user interface guidelines recommend. Custom menus can be created with the "Item" and "Submenu" components.

Figure 3. The Menu Palette

The view palette contains most of the day to day interface components; sliders, buttons, text fields, radio buttons, color well and drop down menu, as well as a CustomView component which is used to represent any subclass of NSView. By using this component, you avoid having to create a new palette for each new view class.

The View Palette

The window palette provides a panel component and a window component. Panels are intended to be used for support dutities, while windows are the primary user interface component.

The Window Palette

The DataView palette gives access to the text view, with support for plaintext, RTF and HTML, as well as to the image view and to the date and number formatters, which can be used with display cells to provide proper formatting of dates and numbers, with localization support.

The DataView Palette

Creating object instances

For objects that don't exist in palettes, you can create instances from InterfaceBuilder's class browser. This is intended for control and model objects, object that support the user interface, but don't provide direct user interaction.

InterfaceBuilder's class browser

EOModeler

Since Enterprise Objects uses the Entity-Association model of databases, EOModeler is used to create an enterprise model from an existing relational database model, or to create an enterprise model, from which EOModeler creates the relational database model.

EOModeller's diagram view of a database model

Enterprise Objects is also the base for WebObjects, and a full exploration of its possibilities would fill many volumes.

Further Reading

There are currently very few books on Cocoa. A decent substitute is "NeXTSTEP Programming, Step One: Object Oriented Applications" by Simson Garfinkel and Michael Mahoney, which is based on NeXTSTEP rather than Cocoa, but will give an introduction to the concepts of dynamic object oriented programming, and to many of the ideas in Cocoa.

Apple provides some good documentation in their developer section, under
<http://developer.apple.com/techpubs/macosx/macosx.html>. Particularly interesting are "Object-Oriented Programming and the Objective-C Language", "OPENSTEP Development Tools & Techniques" and the articles under "Programming Topics". The references for Foundation and AppKit are also suggested reading.

The Objective-C runtime system is part of the Darwin project at <http://publicsource.apple.com>, and is a great help when you need to work with the lowest levels of Objective-C, extending the language itself.
GNUstep, a free implementation of the OpenStep specification is under progress at <http://www.gnustep.org>. The sources should be helpful to anyone wanting to understand the internals of Cocoa.

StepWise (<http://www.stepwise.com>) has an article library with many interesting articles on NEXTSTEP, OPENSTEP and Cocoa programming.

John Hörnkvist is a master student at Chalmers University of Technology in Göteborg, Sweden. He has created a unique academic niche by working on MSc degrees in.both "Computer Science and Engineering" and "Industrial Engineering and Management of Technology". Outside his studies, John has been doing NeXTStep and OPENStep development since 1994, both as an independent developer and as a consultant. His work includes Magnacharta, an application for technical analysis of stock trends, and the Graphickit, a framework for structured graphics. John is also the founder of toastedmarshmallow.com and can be reached as john@toastedmarshmallow.com.

Community Search:

MacTech Search:

Software Updates via MacUpdate

MacFamilyTree 7.3.4 - Create and explore...

MacFamilyTree gives genealogy a facelift: it's modern, interactive, incredibly fast, and easy to use. We're convinced that generations of chroniclers would have loved to trade in their genealogy... Read more

Yummy FTP 1.10.2 - FTP/SFTP/FTPS client...

Yummy FTP is an FTP + SFTP + FTPS file transfer client which focuses on speed, reliability and productivity.
Whether you need to transfer a few files or a few thousand, schedule automatic backups, or... Read more

VueScan 9.5.08 - Scanner software with a...

VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more

Iridient Developer 3.0.1 - Powerful imag...

Iridient Developer (was RAW Developer) is a powerful image conversion application designed specifically for OS X. Iridient Developer gives advanced photographers total control over every aspect of... Read more

Air Video Server HD 2.1.0 - Stream video...

Air Video Server HD streams videos instantly from your computer on your iPhone, iPad, iPod touch or Apple TV. No need to worry about converting or transferring files.
We took everything that was... Read more

Duplicate Annihilator 5.7.5 - Find and d...

Duplicate Annihilator takes on the time-consuming task of comparing the images in your iPhoto library using effective algorithms to make sure that no duplicate escapes.
Duplicate Annihilator... Read more

BusyContacts 1.0.2 - Fast, efficient con...

BusyContacts is a contact manager for OS X that makes creating, finding, and managing contacts faster and more efficient. It brings to contact management the same power, flexibility, and sharing... Read more

Capture One Pro 8.2.0.82 - RAW workflow...

Capture One Pro 8 is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 300 high-end cameras -- straight out of the box. It... Read more

Backblaze 4.0.0.872 - Online backup serv...

Backblaze is an online backup service designed from the ground-up for the Mac.With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

Little Snitch 3.5.2 - Alerts you about o...

Little Snitch gives you control over your private outgoing data.
Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more

Pie In The Sky: A Pizza Odyssey 1.0
Device: iOS Universal
Category: Games
Price: $2.99, Version: 1.0 (iTunes)
Description:
A game about delivering pizza. In space.
| Read more »

Chosen Gives Hopeful Singers, Songwriter...

If YouTube videos and reality TV shows like The Voice have taught us one thing, it’s that there are a lot of people out there who are anxious to show the world their talents. And if they’ve taught us a second thing, it’s that there’s an almost... | Read more »

Android's Popular OfficeSuite Now A...

Once only available for Android devices, OfficeSuite has finally landed on the app store. The Mobile Systems app lets you view, edit, create, and share Word, Excel, and PowerPoint documents as well as convert them to/from PDFs. It's touted as being... | Read more »

Warhammer: Arcane Magic is Coming Soon,...

Turbo Tape Games has announced that they're joining forces with Games Workshop to bring the turn-based strategy board game, Warhammer: Arcane Magic, to life on the iOS.
| Read more »

Fast & Furious: Legacy's Creati...

| Read more »

N-Fusion and 505's Ember is Totally...

| Read more »

These are All the Apple Watch Apps and G...

The Apple Watch is less than a month from hitting store shelves, and once you get your hands on it you're probably going to want some apps and games to install. Fear not! We've compiled a list of all the Apple Watch apps and games we've been able to... | Read more »

Appy to Have Known You - Lee Hamlet Look...

Being at 148Apps these past 2 years has been an awesome experience that has taught me a great deal, and working with such a great team has been a privilege. Thank you to Rob Rich, and to both Rob LeFebvre and Jeff Scott before him, for helping me... | Read more »

Hands-On With Allstar Heroes - A Promisi...

Let’s get this out of the way quickly. Allstar Heroes looks a lot like a certain other recent action RPG release, but it turns out that while it’s not yet available here, Allstar Heroes has been around for much longer than that other title. Now that... | Read more »

Macho Man and Steve Austin Join the Rank...

WWE Immortals, by Warner Bros. Interactive Entertainment and WWE, has gotten a superstar update. You'll now have access to Macho Man Randy Savage and Steve Austin. Both characters have two different versions: Macho Man Randy Savage Renegade or Macho... | Read more »

Price Scanner via MacPrices.net

Adobe Brings Powerful Layout-Design Capabilit...

Adobe today announced the availability of Adobe Comp CC, a free iPad app that enables rapid creation of layout concepts for mobile, Web and print projects. With Comp CC, designers can rough out and... Read more

Apple offering refurbished 27-inch 5K iMacs f...

The Apple Store is offering Apple Certified Refurbished 27″ 3.5GHz 5K iMacs for $2119 including free shipping. Their price is $380 off the price of new models, and it’s the lowest price available for... Read more

16GB iPad mini on sale for $199, save $50

Walmart has 16GB iPad minis (1st generation) available for $199.99 on their online store, including free shipping. Their price is $50 off MSRP. Online orders only.
Read more

The Apple Store has Apple Certified Refurbished 13″ 2.6GHz/128GB Retina MacBook Pros available for $979 including free shipping. Original MSRP for this model was $1299.
Read more

Save up to $600 with Apple refurbished Mac Pr...

The Apple Store is offering Apple Certified Refurbished Mac Pros for up to $600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more

Samsung Galaxy S 6 and Galaxy S 6 edge U.S. P...

Samsung Electronics America, Inc. has announced the Galaxy S 6 and Galaxy S 6 edge will be available in the U.S. beginning April 10, with pre-orders being accepted now.
“We have completely reimagined... Read more

13-inch 2.5GHz MacBook Pro (refurbished) avai...

The Apple Store has Apple Certified Refurbished 13″ 2.5GHz MacBook Pros available for $829, or $270 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free:
- 13″ 2.... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.