OS X Development

mewlingtincupSoftware and s/w Development

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

111 views

OS X Development



Tom Schroeder

Software Engineering

University of Wisconsin
-
Platteville

schroederjo@uwplatt.edu



Abstract


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
increasing
. At
the core of OS X applications are

the Cocoa framework and the language
Objective
-
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
-
View
-
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
,
and

ultimately provide
s

an effective and comparatively superior means to create desktop applications.





Introduction


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
[5]
.


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
platf
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.



History


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.
NeXTSTEP
consisted of several pieces, such as Objective
-
C and its runtime, several application
2


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

[6]
. 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
NeXTSTEP.



Objective
-
C


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

Objective
-
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
Smalltalk
-
esque mentality of message passing, as well a
s a thin but powerful runtime.



History


Objective
-
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

[4]
. 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.

Objective
-
C now
ranks third on the TIOB
E Programming Community Index
[10]
.



Messages


Objective
-
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

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


[array
objectAtIndex
:
0
];

Figure 1: Passing a message.


Figure 1 demonstrates the passing of the
objectAtIndex

message to
an

NSArray

object. This will
return the object at the 0
th

index of
array
.



Interfaces and Implementations


3


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
declaration


of
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.



Properties


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
accessors

for the property

created using the “synthesize” directive.

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


@property

(
weak
)
IBOutlet

NSTextField

*textField;

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”

[7]
.



Object Life C
ycle


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
“retain
-
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
eference
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

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



Figure 3
:
Retain
-
Release Cycle.


4




Protocols


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

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

and
C#
. In a class interface,
the declaration specifies that the class
adopts

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


@interface

ViewController :
NSViewController

<
NSTabViewDelegate
>

Figure 4
: Declaring conformity to protocol (NSTabViewDelegate)
.




Categories


In being a dynamic and object
-
oriented language, Objective
-
C supports open classes.
Open
classes are in essence classes that allow for methods to be added to them, including “built
-
in”
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.


@interface

NSString

(Utilities)

Figure 5
: Declaring category.




Dynamic Typing


Much of Objective
-
C’s power comes from dynamic typing. Using the
id
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.


id

items =
@[
[
NSMutableSet

class
], [
self

superview
],
@"Five"
]
;

Figure 6
: Dynamic typing example with
id

and NSArray.


5


In Figure 6, an NSArray is created and

items


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



Cocoa


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
ostensibly
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
-
View
-
Controller) paradigm make the classes
relativel
y simple to use.



6


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
data
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.
Also,
using the “Managed Objects” included in Core Data, the
command

design pattern
(undo/redo)
can be easily implemented, with much of the detail abstracted from the implementer.



Environment


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
nstruments,
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
entire
projects to be implemented without clutter breaking concentration.


The
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,
Objective
-
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
suc
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
-
and
-
drop graphical components,
7


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
on,
Interface Builder provides a way to integrate sub
-
classed Cocoa graphical components into the
interface.



Design


The Model
-
View
-
Controller design paradigm is very important in OS X
development,

as MVC
is the basis of the Cocoa framework.

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

The Model
-
View
-
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
system.
The interaction and message sending are shown in Figure 6.
In Cocoa, views are
i
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
-
View
-
Controller State Description and Message
Passing
.
[7]



Cocoa
utilizes

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
8


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
s

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

The
Observ
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
Responsibility

(NSResponder and AppKit Responder Chain)
, Co
mposite

(view hierarchy)
,
Decorator

(categories)
, Façade

(NSImage)
,
Iterator (fast enumeration),
Mediator

(controller
classes)
, Memento

(archiving and serialization)
,
and
Proxy

(NSProxy)

[1
]
.



Conclusion


Developing OS X applications is a well
-
established

process with superb tools and frameworks.
As always, Apple has created their own unique experience and ideology, but Cocoa, Objective
-
C,
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.



References


[1]
Apple. (2010, December 13).

Cocoa

fundamentals guide
. Retrieved from

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFunda

mentals/Introduction/Introduction.html

[2]
Apple. (2011, October 12).

The objective
-
c programming language
. Retrieved from

https://dev
eloper.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/I

ntroduction/introObjectiveC.html

[3] Apple. (2012
).

What’s new in Xcode 4
. Retrieved from

https://developer.apple.com/technologies/tools/whats
-
new.html

9


[4]
Biancuzzi, F., & Warden,
S. (2009).

Masterminds of programming: Conversations with the

creators of major programming languages

. Sepastopol, CA: O'Reilly Media.

[5]

Gartner. (2012, January 11
).

Gartner Says Worldwide PC Shipments in Fourth Quarter of

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


http://www.gartner.com/it/page.jsp?id=1893523

[6]

Kaul, Vivek. (2009, May 11
).

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


http://www.dnaindia.com/money/report_what
-
steve
-
jobs
-
did
-
when
-
he
-
was
-
fired
-
from
-

apple_1254757

[7]

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

A description of the model
-
view
-
controller user interface

paradigm in the smalltalk
-
80 system
. Retrieved from

http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf

[8]

LLVM. (2012
).

Automatic Reference Counting
. Retrieved from


http://clang.llvm.org/docs/AutomaticReferenceCounting.html

[9]

Stevenson, S. (2010).

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

California: O'Reilly Media, Inc.

[10]

TIOBE Software. (2012
,

November
).

TIOBE programming community index for November

2012
. Retrieved from

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html