OS X Development

mewlingtincupSoftware and s/w Development

Nov 9, 2013 (3 years and 5 months ago)


OS X Development

Tom Schroeder

Software Engineering

University of Wisconsin



Apple’s Mac and OS X platform
s are increasing in popularity as
the complementing counterpart
to iOS. With Apple’s growing grasp
on the personal computer market, the importance of
software professionals having an understanding of the platform and its development

is constantly
. At
the core of OS X applications are

the Cocoa framework and the language
C. Apple provides the development environment, Xcode, which
contains many
powerful applications such as the LLVM engine and Interface Builder.

Developing software
using Cocoa, Objective
C, and Xcode requires
knowledge of many design patterns. The Model
Controller pattern
is an integral part of Cocoa and OS X applications, as well as many
other design patterns such as the Command pattern and the Observer pattern.

Developing OS X
applications is a unique ex
perience and employs techniques not seen in other environments

ultimately provide

an effective and comparatively superior means to create desktop applications.


In the past few years, Apple has emerged as a premier company in hardwar
e and software design.
Apple’s Mac and OS X platforms are increasing in popularity
, with a growth in market
share of
20% from 2010 to 2011

With the launch of the iPhone and other iOS devices, Apple has
cornered a good chunk of the mobile platform demographic. Apple success comes with merit, as
their products always meet or exceed the highest standards in the market. Apple’s Mac and OS X
orms are no different. With masterfully designed hardware and intuitive yet powerful
software, these platforms merit the same acclaim received by their iOS counterpart. Part of this
success comes from Apple’s development suite,
which includes
Xcode and Int
erface Builder, as
well as frameworks such as Cocoa, Core Animation, Core Audio, and Core Data.


When Steve Jobs left Apple in 1985, he founded the company NeXT. Among the various
products developed by NeXT was NeXTSTEP, an object
oriented, mult
itasking operating system.
consisted of several pieces, such as Objective
C and its runtime, several application

layer frameworks, and development tools. Jobs returned to Apple when NeXT was purchased by
Apple in 1996

. Many of NeXT’s technolog
ies found their way into Apple products, most
notably NeXTSTEP’s evolution into Mac OS X. In 2001, Apple released Mac OS X Cheetah,
beginning the lineage of OS X that we encounter today. In 2012, Apple released OS X 10.8
(Mountain Lion), the ninth iteratio
n of OS X, continuing a technology that originated with


The Objective
C programming language is an object
oriented language
comprised of the ANSI C
language and a small set of
extensions to C.

is a
strict superset
of C, inheriting

much of its syntax from C as well as primitive types (i
e. int, char) and flow
control statements. In

addition, Objective
C adds object
oriented constructs

utilizing a
esque mentality of message passing, as well a
s a thin but powerful runtime.


C was created in the early 1980s (by Brad Cox and Tom Love at Stepstone) in an
effort to design a language that facilitated reusability in software design and implementation
while maintaining the
backwards compatibility of C

. In the late 1980s, Objective
C was
popularized by NeXT’s decision to extend the GCC compiler to include Objective
C and to
implement the core frameworks used to design NeXTSTEP, and later Mac OS X and iOS.

C now
ranks third on the TIOB
E Programming Community Index


C has an object model derived from
Smalltalk paradigms. Any operation involving an
object will utilize an implementation of messa
ge passing. In message pas
sing, a method is not
called; rather, a message is sent and the target and interpretation of the message are done at
runtime. This differs from a language like C++, where methods are bound to a target class at
compile time. Message passing is essential for

C to allow for dynamic typing,
postponing a message’s destination until runtime.
Passing a method to an object in Objective
is done
using a bracket syntax similar to Smalltalk.


Figure 1: Passing a message.

Figure 1 demonstrates the passing of the

message to


object. This will
return the object at the 0

index of

Interfaces and Implementations


A class is defined by an interface and an implementation. The interface of a class

contains the
declaration of instance variables, properties, and methods, as well as the

inheritance and protocol extension.

The implementation of a class contains
the implementation
of all methods of the class, including those specified in

the class interface.


Contained in the interface and implementation of a class are properties, which allow

a simple
way to declare and implement the accessor methods for an instance variable
. The syntax allows
the variables to be declared wit
h many keywords, such as “readonly” and “nonatomic”, which
affect the generated

for the property

created using the “synthesize” directive.

example, the “readonly” attribute tells the compiler to only implement the “getter” method and
not the
In summary, properties allow for automatic generation of attribute accessors,
much like Ruby.





Figure 2: Declaring a property.

Figure 2 demonstrates the declaration of a property for an
NSTextField. The “weak” attributes
means that when reading the value “the current pointee is retained and then released at the end of
the current full
expression” and when assigning the value “the lvalue is updated to point to the
new pointee, unless the n
ew pointee is an object currently undergoing deallocation”


Object Life C

An object

s life cycle
consists of the object being allocated, initialized, and finally deallocated.
In between being allocated and deallocated, the object goes through a
release” process

(shown in Figure 3)
, a convention designed to deallocate an object when released for each time
the object is retained. This “retain
release” process is now referred to as Manual R
Counting (MRC), as LLVM now supports Automa
tic Reference Counting (ARC). With ARC,
the compiler determines where each object should be retained and released, allowing the
programmer to omit the retain and release messages.
The benefits of using ARC far outweigh the
drawbacks; the code is faster, cl
eaner, and safer

. ARC provides many of the benefits seen
with garbage collection, without the runtime overhead cost.

Figure 3
Release Cycle.



C is a fully object
oriented programming language, and beside class inheritance,

contains the ability to specify that a class implement a specific list of methods. In
, this convention is called a protocol. Protocols are si
milar to “interfaces” in Java

. In a class interface,
the declaration specifies that the class

the protocol, and the
implementation of the adopted methods of the protocol is referred to as conforming to the


ViewController :


Figure 4
: Declaring conformity to protocol (NSTabViewDelegate)


In being a dynamic and object
oriented language, Objective
C supports open classes.
classes are in essence classes that allow for methods to be added to them, including “built
framework classes, such as NSString.
Open classes allow for a class to be “opened up” and given
new functionality that the user feels should be included. F
or example, complex functionality can
be added to NSString and the execution of that functionality will simply require passing the
message to that object, instead of passing the object to a new function.
The basis of this feature
meshes with the philosophy

of Objective
C: the design, implementation, and maintenance of
large, complex systems.




Figure 5
: Declaring category.

Dynamic Typing

Much of Objective
C’s power comes from dynamic typing. Using the
type, messages can be
passed to objects without explicit knowledge of the object’s true type. Then, the object can
handle the message and respond, drop the message, or send the message on to another object, a
behavior known as message forwarding. Message f
orwarding, as well as delegation, gives a third
option to objects handling messages and can greatly simplify design patterns such as the
Observer pattern and the Proxy pattern.


items =

], [


Figure 6
: Dynamic typing example with

and NSArray.


In Figure 6, an NSArray is created and


is assigned reference to the NSArray. The items
the array are each of a different type because NSArray holds items of type
, which allows for
the dynamic


The power behind OS X applications and their development is the Cocoa framework. The Cocoa
framework provides applications the distinctive look and feel often associated with OS X. Cocoa
actually consists of three separate frameworks: Foun
dation Kit, Application Kit, and Core Data.
Together, these three frameworks provide a set of basic classes (such as NSString),
a collection
of graphical user interface entities
, and a method for handle persistent data models.

Foundation Kit

Foundation Kit, also referred as Foundation, is a collection of base layer classes, defined loosely
as objects that do not appear in or exclusively support the user interface. Applications, such as
command line tools, can be created solely using Foundation
. Foundation was designed to
implement or facilitate the following functionalities: define general object behavior and life cycle,
support localization,
support portability, and provide object wrappers for programmatic
primitives and operating system level

functions, such as ports, files, and threads.

Foundation Classes

Foundation contains over 100 different classes. Arguably most important, Foundation contains
NSObject, the root class that defines all basic behaviors for all objects in Cocoa (and
Apple’s implementation of Objective
C). Of the many cla
sses in the Foundation Kit, Foundation
contains relevant classes such as NSString, NSArray,
NSThread, NSDate, and NSURL

As a note, due to Objective
C’s lack of namespaces, class names are given a prefix in order to
differentiate. In the case of Foundation, that prefix is “NS”, which stands for NeXTSTEP.

Application Kit

Application Kit, also referred to as AppKit, contai
ns all of the objects required to create a
graphical application for OS X. These objects include the windows, buttons, scroll boxes, and
text fields. AppKit handles much of the actions required
such as efficiently rendering the
windows and views for the ap
plication. AppKit does this with its more than 125 classes. The
number of classes might seem overwhelming (or overly complicated), but the intuitive class
hierarchy and strict adherence to the MVC (Model
Controller) paradigm make the classes
y simple to use.


Using Interface Builder
, the software developer can create connections between views (NSView)
and controllers (such as NSWindowController and NSViewController). The controllers then
manipulate and control the data structures defined by t
he implementer. The power of AppKit
comes from the extensibility of these views and controllers
using inheritance to perform custom
functionality such as specialized drawing or actions.

The developer may also create custom
components but sub
classing an AppKit component and overriding actions in order to customize
the component.

Core Data

The Core Data framework is the third piece of Cocoa. Core Data provides a way to manage the
of an application, referred to as the “data model”, including the serialization and persistence
of that data. Core Data instigates the serialization of data using XML, binary, and SQLite.
using the “Managed Objects” included in Core Data, the

design pattern
can be easily implemented, with much of the detail abstracted from the implementer.


In order to foster an excellent development experience, a software suite is often used to combine
all of the elements, such as
the frameworks, language, and build toolchain

(a collection of
programs use to compile and assemble the developed software)
. For Apple, that integrated
development environment is Xcode. The
power and elegance of Xcode set

it apart from other
IDEs, with the

single window interface, the Apple LL
VM engine, the version editor, I
and the built
in Interface Builder application.

Xcode employs an interface that allows all common development tasks to be performed from a
single, consolidated window. This
single window provides a transparent interface and allows
projects to be implemented without clutter breaking concentration.

Apple LLVM engine

is the core of Xcode. The LLVM compiler compiles code very fast,
and produces fast applications. LLV
M, from the ground up, fully supports C,
, and
C++. LLVM handles code completion and syntax highlighting, interfacing with Xcode to display
this information. Also, LLVM dynamically analyzes code as it is written and identifies “live”
issues. The

“live” issues are not

only identified, but also explained

and often automatically fixed.
An “analyze” feature is also included to find deeper bugs, such as memory leaks.

Xcode has a number of built
in utilities to enhance the development process.
Version control is
an integral part of the software development process and Xcode can help
use configuration
management utilities
h as Subversion and G
it with it
s Version Editor.

Along with Version
Editor, Instruments is a set of tools for dynamically a
nalyzing the performance of an application
with tools for monitoring CPU, memory, and file system activity.

Finally, Interface Builder
is the development environment used to construct the visual interface
for Cocoa applications.
Interface Builder allows
the user to drag
drop graphical components,

such as buttons and views, onto the application’s window to create a dynamic and featured
experience. Interface Builder also provides a way to connect IBOutlets and IBActions to the
code of the application. I
BOutlets are objects, usually found in view controllers that are directly
related to components in the graphical interface. IBActions are methods that can be hooked to
graphical interface components, such as the response to the click of a button. In additi
Interface Builder provides a way to integrate sub
classed Cocoa graphical components into the


The Model
Controller design paradigm is very important in OS X

as MVC
is the basis of the Cocoa framework.

Specifically, AppKit and Core Data rely heavily on the
MVC design paradigm.

The Model
Controller architecture divides a system into three
component types. The model
is the domain
specific data and operations for the application. The
view is responsib
le for graphically displaying the model.
The controller is responsible for
creating the interface between the model and the view, as well handling interaction with the
The interaction and message sending are shown in Figure 6.
In Cocoa, views are
mplemented with the NSView class and controllers are implemented with the
NSViewController class. Models are implemented at the programmer’s discretion, usually as a
subclass of NSObject.

Figure 7
: Model
Controller State Description and Message


the Delegation pattern to pass notifications and other messages to the controller
and the model from the view. Using protocols, a class can be defined to implement the proper
methods needed to act as the delegate. In this way,

a view controller can be defined as a delegate
for a view component, as receive asynchronous notifications when the component encounters
certain states,

such as when a user

begins editing a text field.
The power of the delegation

mechanism is in its simpl
icity. A delegating class will have a delegate property that any class
that implements the proper protocol can be assigned to. Once this assignment is done, the
delegate will now receive all delegated messages.

Many popular design patterns are utilized
and/or facilitated by the architecture and mechanisms
of Cocoa.

The inclusion of these design patterns creates a more mature environment and one
with a greater familiarity for software developers.

The Command pattern represents an action as an object, all
owing interaction with the action
before and after execution. The Foundation framework contains NSInvocation, a class that
encapsulates an Objective
C message and represent

that message as an object.
An invocation
object can be used to implement the undo
/redo pattern, manage action timing, and more.

er pattern is uti
lized with the Key
Value Observing (KVO) mechanism in which a view
object receives notifications with a relative variable is changed, and vice versa. Cocoa contains
classes the follo
w the Singleton pattern, such as NSFileManager and NSApplication
, allowing
convenient access to these classes without managing instances. The Adapter (wrapper) design
pattern is seen in the Objective
C feature of protocols, which allows the class to be ext
ended to
interface with classes not strictly associated. Also, Cocoa utilizes or facilitates the following
design patterns: Abstract Factory

(class clusters such as NSString and NSArray)
, Chain of

(NSResponder and AppKit Responder Chain)
, Co

(view hierarchy)

, Façade

Iterator (fast enumeration),

, Memento

(archiving and serialization)




Developing OS X applications is a well

process with superb tools and frameworks.
As always, Apple has created their own unique experience and ideology, but Cocoa, Objective
and Xcode are highly accessible and provide a powerful and effective way to create superb
software applications.

The u
ser base of OS X is constantly growing.

Possessing knowledge of
these tools, as well as the design patterns u
tilized by Cocoa, will allow a software developer to
create and maintain any OS X application.


Apple. (2010, December 13).


fundamentals guide
. Retrieved from



Apple. (2011, October 12).

The objective
c programming language
. Retrieved from



[3] Apple. (2012

What’s new in Xcode 4
. Retrieved from



Biancuzzi, F., & Warden,
S. (2009).

Masterminds of programming: Conversations with the

creators of major programming languages

. Sepastopol, CA: O'Reilly Media.


Gartner. (2012, January 11

Gartner Says Worldwide PC Shipments in Fourth Quarter of

2011 Declined 1.4 Percent;
End Shipments Increased 0.5 Percent
. Retrieved from



Kaul, Vivek. (2009, May 11

What Jobs did when he was fired from Apple
. Retrieved from




Krasner, G., & Pope, S. (1988).

A description of the model
controller user interface

paradigm in the smalltalk
80 system
. Retrieved from



LLVM. (2012

Automatic Reference Counting
. Retrieved from



Stevenson, S. (2010).

Cocoa and objective
c: Up and
. (1st ed.). Sebastopol,

California: O'Reilly Media, Inc.


TIOBE Software. (2012


TIOBE programming community index for November

. Retrieved from