More Hello Cocoa!

This is the second iteration of a Hello World Application in Cocoa, enhanced with a sidebar typical of many Mac applications. Apple say “If you’re new to OS X app development, your first step is to become an OS X user yourself” and this is reflected in my approach to learning Cocoa User Interface Design, where I have looked to the standard applications included with OS X such as iPhoto for ideas about design. This App Style is referred to as a Library application.

Hello Cocoa 2

This application is structured so that the main window and each view have a separate controller/XIB and communication between controllers occurs in a way that is considered good object oriented design. To achieve this the sidebar must be able to send messages related to selection changes without actually knowing the recipient of these calls. The purpose of this approach is to achieve a design that is decoupled and unit testable.

To achieve this in Objective-C it is necessary to use a Protocol and inject a reference to the main window into the Sidebar. This design is an implementation of the Dependency Inversion Principle as described in SOLID Object Oriented Class Design. The code snippet below shows the implementation for the Sidebar View Controller initialiser accepting an input of type id typed to HWNavigating, which is the protocol to be implemented by the main window and assigning this to a local variable for later use.

- (id)initWithNavigatingManager:(id<HWNavigating>)navigating {    
    self = [super initWithNibName:@"SidebarView" bundle:nil];    
    if (self) {  
        self->navigatingManager = navigating; // Set the Injected Dependency
    }
    return self;
}

 

With the continued evolution of OS X, it’s a great time for software developers to start building Cocoa Applications, though even if Apple say that “Objective-C is a simple language with syntax and conventions that are easy to learn—especially if you have experience with other object-oriented languages…” some effort is still required! You can read about the features that Objective-C adds to C to begin the journey along the long and winding road to becoming an accomplished Apple Developer.

The frustrations I’ve found in learning Cocoa would be common to anybody learning a new programming environment and include trying to find approachable resources that are up to date. Reading eBooks has provided a focused way to get started, followed by the WWDC videos for the current and previous years.

Download source (1.7 MB)

Published by Mark Brownsword

I've been working as an enterprise developer for about 15 years, always using .NET platform and mostly using Oracle databases. My degree is in Business Studies, majoring in information systems, where I learnt the fundamentals of software engineering for building systems for business.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s