Getting Started with OS X/iPhone Development – Part 5: Frameworks

In this article we will continue our discussion of OS X and iPhone development by extending our application.  The vehicle we will use will be what is called the framework.

What are frameworks?

Most people are familiar with the standard packaging of support libraries.  Libraries, header files, etc. are always distributed and are meant to be integrated into your project and build process.  Typically these will be found in UNIX-styled directory structures with the header files in “include” or “inc” directories and their relative binaries often under “lib”.

What frameworks do is provide a standardized way of distributing multiple releases of a support library in a single package with predictable layouts.

In our “MyButtonBox” application we started by creating a project based on the “Cocoa Application” template. This template already includes several frameworks that we have been using all of this time.  These are all standard OS X frameworks that ship as part of your OS.  They are:

  • Cocoa.framework – standard Cocoa elements
  • AppKit.framework – application-level components
  • CoreData.framework – data management components
  • Foundation.framework – the best.  The low-level structures and utility classes.

There are two places where you will commonly find frameworks:

/System/Library/Frameworks

This directory contains frameworks that are installed as part of the OS.

/Library/Frameworks

This is the standard installation location for third-party frameworks that have been installed by the users or their applications.

Adding a framework to your project

Let’s try adding a simple framework to our “MyButtonBox” project and integrating with it.  If you don’t have the project open in XCode, open it now and examine the project tree on the left side of the screen.  Look for a container called “Frameworks”.

  • Right-click the “Frameworks” container.
  • From the pop-up menu, pick “Add > Existing Frameworks…”.
  • Navigate to “/System/Library/Frameworks”.
  • Select the framework “Tcl.framework”.
  • Click the the “Add” button.

The framework to support the language TCL has been added to your project.  You can now include the master header file in your code.  The master header is a header file that usually includes all others in the framework.  This makes integration much less tedious when it is there.  It isn’t always.  Sometimes you’ll have to dig around in the framework the old fashioned way.  For now, return to “MyController.m” and insert the line:

#import <Tcl/Tcl.h>

This will include the master file for the TCL framework and change the body of your “myButtonWasPressed:” method to the following.

- (IBAction)myButtonWasPressed:(id)sender
{
    NSString *whatWasTypedIn = [myInputField stringValue];

    if ([whatWasTypedIn length] > 0)
    {
        Tcl_Interp *interp = Tcl_CreateInterp();
        Tcl_Init(interp);
        Tcl_Eval(interp, [whatWasTypedIn UTF8String]);
        NSString *msg = [NSString stringWithUTF8String:Tcl_GetStringResult(interp)];
        [myOutputField setStringValue:msg];
        Tcl_DeleteInterp(interp);
    }
}

Our code will now read the input from the text field, interpret it as a line of TCL code and output the result in our output field.  We should now be able to build and run in the TCL-enabled version of our code.

  • Press cmd-S to save the project.
  • Press cmd-B to build it.
  • Press cmd-R to run it.

Now try typing some single-line codes in the input field and pressing the button.  Try typing in “set a 5″.  The result “5″ should appear in the top display.

Congratulations.  You’ve added a framework to your project.  We’ll add a few more embellishments to our UI before moving on to more complex issues.

BACK TO: Neon Hue Software

Contents copyright (c) 2010 Neon Hue Software. All Rights Reserved. No part of this document may be copied or republished without prior written consent. Please contact corporate@neonhuesoftware.com if you wish to re-publish this content on your site or in any other form.

Leave a comment

You must be logged in to post a comment.