TweetFollow Us on Twitter

Digging in to Objective-C

Volume Number: 19 (2003)
Issue Number: 2
Column Tag: Getting Started

Digging in to Objective-C

by Dave Mark

In this month's column, we're going to start our foray into Objective-C. Before we do that, however, let's make sure we're on the same page, tools-wise.

Got the Latest Tools?

If you haven't already, login to http://developer.apple.com/ and download the December 2002 Mac OS X Developer Tools. Note that the download is 301.2 MB so maybe let this one run during your lunch break. Note that the December 2002 Tools will only run on 10.2 or later. If you need to run on an earlier OS, stick with the April 2002 Tools.

Once your download is complete, you'll have a very large file named Dec2002DevToolsCD.dmg. Double-click this. The disk image that mounts will contain a file named Developer.mpkg. This master package will install all the packages in the Packages folder. No need to install these separately.

The install will take a while. Be patient. There's a lot of stuff here. Here's a list of the packages that will be installed as part of this process:

    - Developer Tools (DevTools.pkg)

    - Mac OS X SDK (DevSDK.pkg)

    - Developer Documentation (DevDocumentation.pkg)

    - Developer Examples (DevExamples.pkg)

    - SDK pieces for UNIX development (BSDSDK.pkg)

    - A package with extra pieces needed for Project Builder-emacs integration (Dec2002DevToolsExtras.pkg)

If your setup was the same as mine, you were running Project Builder 2.0.1 and are now running Project Builder 2.1. This is from the Project Builder 2.1 section in the file What's New.pdf:

    - Support for external editors:

    * You can use arbitrary editors for different file types.

    * Project Builder will show you build errors in your external editor when you use BBEdit or emacs.

    - Build system features:

    * Project Builder will now do multiple compiles at the same time on machines with more than one processor.

    * Project Builder's header dependency analysis is improved.

    * Project Builder now links executables in a more optimal manner.

    * You can now set compile flags on a per-file basis.

    * Assembler flags are now separate from flags to the compiler.

    - Interface to customize key bindings.

    - Better CVS integration.

    - The CodeWarrior project importer has been vastly improved.

    - Miscellaneous interface improvements:

    * You can now sort files, targets, and build phase objects in a project.

    * You can display line numbers on the same line as your source.

    * The breakpoint gutter has been improved.

    - JavaDoc documentation is now indexed so that it can be used with Project Builder's integrated documentation.

    - Improved text encoding support.

    - There is support for dynamic C++ types in the debugger.

In addition, here's the non-Project Builder specific stuff:

    * gcc3.1 has several bug fixes. In addition:

    - an optimization has been made to speed up searching for header files. If your command lines have a lot of -I directives to add directories to the compiler's search path, this optimization should help your compile time.

    - The HTML documentation for the compiler is missing from the release. You can find it on the web at:

    http://developer.apple.com/techpubs/macosx/DeveloperTools/gcc3/gcc/index.html

    http://developer.apple.com/techpubs/macosx/DeveloperTools/gcc3/cpp/index.html

    * AppleScript Studio 1.2.1 adds enhanced dictionary support. The sample projects will now run on both Mac OS X 10.1 systems as well as Mac OS X 10.2. Please read the AppleScript Studio release notes for more information.

    * CHUD 2.5.1 is now on the CD. CHUD is a set of tools for low-level system debugging and profiling. To install, open the disk image, and follow the instructions.

    * !AppleScript Editor 2.0 Beta is now available in the Pre-release Software folder. Please read the "About These Packages.rtf" file in the Pre-release Software folder for more details. Note that this package requires Mac OS X 10.2.3 or higher.

    * The jikes Java compiler has been updated to version 1.17.

    * PackageMaker has some new features:

    - Drag-and-drop functionality in the list editor.

    - Inline attribute editing.

    - Progress sheet for status during package creation.

    - Package completion dialog.

    * Sherlock SDK - There is now an SDK available to develop your own Sherlock channels. See

    /Developer/Examples/Sherlock for sample channels and the online documentation for more information.

    * DiscRecording SDK - There are now sample projects demonstrating the DiscRecording framework. Please see: /Developer/Examples/DiscRecording/About Disc Recording Examples.rtf for more information.

    * CoreAudio SDK - There are more examples in /Developer/Examples/CoreAudio. Please see /Developer/Examples/CoreAudio/ReadMe.rtf for more information.

    * FireWire SDK - There is now a complete SDK for developing for FireWire devices. Please see

    /Developer/Examples/IOKit/firewire for the samples.

    * ForceFeedback SDK - The header files necessary to take advantage of Mac OS X 10.2.3's ForceFeedback framework have been added.

    * AvailabilityMacros - You can now control which OS version you want to target your application for using the AvailabilityMacros. Please see

    /Developer/Documentation/ReleaseNotes/AvailabilityMacros.h for more details.

    * New and updated documentation. Lots of new documentation content is included in this release. Please check the various "what's new" portions of the documentation for more information.

When you launch Project Builder 2.1 for the first time, the release notes window appears. You can bring this window up again at any time by selecting Show Release Notes from Project Builder's Help menu.

On to Objective-C...

OK. Now that we're all using the same tools, let's dig into our main course. Objective-C is based on the C programming language. In effect, it is C with objects. Though there are many differences between Objective-C and C++, the main difference lies in the dynamic nature of Objective-C.

C++ requires static dispatch, static typing, and static loading. Objective-C offers dynamic dispatch, dynamic typing, and dynamic loading. In a nutshell, static dispatch means the decision of which object receives which message happens at compile-time, while dynamic dispatch means the decision is delayed until run-time.

Same thing with typing and loading. When the type of the message receiver can be determined at run-time, that's dynamic typing. When code can be linked and executed at run-time as opposed to at compile-time, that's dynamic loading.

To me, all three of these concepts are very tightly intertwined. A static approach offers the safety of forcing a lot of problems to the surface at compile-time. Don't be lulled into a false sense of security, however. Just because your code compiles doesn't mean your code isn't riddled with errors. It just means that your code conforms, at a basic level, to the language syntax enforced by the compiler. But static typing, for example, clearly offers a certain level of safety not afforded by dynamic typing.

The dynamic approach jettisons the static safety net in exchange for incredible flexibility and freedom. Dynamic loading means you can deliver a new suite of objects to your customer over the net, and have the code installed, all while the existing app is still up and running. Imagine the possibilities. You could fix a bug on a mission-critical program without having to shut it down. If you knew the protocol, you could even patch someone else's program, replacing their inferior object with one of your own design.

To be fair to C++, there is a certain element of risk in a completely dynamic approach. But with this risk comes great potential reward. Whichever side of the dynamic/static argument you come down on, there is no downside to learning and understanding Objective-C. If you understand the benefits of Objective-C, you can make an informed choice between it and C++. And the cool thing is, with Objective-C, you can still use static typing when you choose to (to make an element of your code clearer to others, for example). With Objective-C, you get the best of both worlds.

If all this seems a little vague, not to worry. Over time, as we dig into more and more code, these concepts will fall into place. Let's take a look at an example.

Employee: The C++ Version

To start things off, here's a relatively simple C++ program (lifted and slightly modified from Learn C++ on the Macintosh, with permission from the author!) that we'll translate into Objective-C. We're going to define a class named Employee, then create and destroy a couple of Employee instances.

Want to follow along in Project Builder? Create a new project, and select "C++ Tool" from the "Tool" category when prompted for the type of project to build. Now select New File from the File menu and choose "C++ Class" from the "Carbon" category. You'll call the new class "Employee". Project Builder will create two new files (assuming you checked the "Also create Employee.h" checkbox) and add them to your project. All you need to do is replace the contents of Employee.h, Employee.cpp, and main.cpp with the code below.

    Got a net connection? Rather download the code than type it in? We're working on adding the code downloads to the MacTech web site. If that is not up and running by the time you read this, head over to http://www.spiderworks.com and download the code from there. I'll put both the C++ and Objective-C versions up for your downloading pleasure.

Here's Employee.h:

class Employee
{
//         Data members...
   private:
      char   employeeName[ 20 ];
      long   employeeID;
      float   employeeSalary;
//         Member functions...
   public:
            Employee( char *name, long id, float salary );
            ~Employee( void );
      void   PrintEmployee( void );
};

Here's Employee.cpp:

#include <iostream>
#include "employee.h"
Employee::Employee( char *name, long id, float salary )
{
    strcpy( this->employeeName, name );
    this->employeeID = id;
    this->employeeSalary = salary;
    
    std::cout << "Creating employee #"
                           << employeeID << "\n";
}
Employee::~Employee( void )
{
    std::cout << "Deleting employee #"
                           << employeeID << "\n";
}
void   Employee::PrintEmployee( void )
{
    std::cout << "-----\n";
    std::cout << "Name:   " << employeeName << "\n";
    std::cout << "ID:     " << employeeID << "\n";
    std::cout << "Salary: " << employeeSalary << "\n";
    std::cout << "-----\n";
}

Finally, here's main.cpp:

#include <iostream>
#include "employee.h"
int main (int argc, const char * argv[])
{
    Employee   employee1( "Frank Zappa", 1, 200.0 );
    Employee   employee2( "Donald Fagen", 2, 300.0 );
    employee1.PrintEmployee();
    employee2.PrintEmployee();
}

Once all the code is typed in, give it a run and compare your results to those shown in Figure 1. If you compare the results with the code in main.cpp, you'll see that the first two lines are produced as the two Employee objects are created, the next two chunks (5 lines each) are produced by the two calls to PrintEmployee(), and the two "Deleting" lines are produced when the program exits and the objects are destroyed.


Figure 1. The C++ version of Employee in action.

The Employee object is fairly straight-forward. Looking at the code in Employee.cpp, you'll see that the constructor uses its arguments to initialize the data members of the Employee object, then uses cout to send a message letting us know the object is being created. The destructor also uses cout to let us know its object is being destroyed. And PrintEmployee() uses cout to print some useful info as well.

Pretty straight-forward, right? Now let's create an Objective-C program to do the same thing.

Employee: The Objective-C Version

In Project Builder, select New Project... from the File menu. This time, select "Foundation Tool" from the Tool category. Now select New File... from the File menu and choose Objective-C Class from the Cocoa category. Name the new file Employee.m and be sure the "Also create Employee.h" checkbox is checked.

As you did with your C++ project, now you'll replace the contents of Employee.h, Employee.m, and main.m with the code below. Here's Employee.h:

#import <Foundation/Foundation.h>
@interface Employee : NSObject {
    @private
        char      employeeName[20];
        long      employeeID;
        float      employeeSalary;
}
- (id)initWithName:(char *)name andID:(long)id
    andSalary:(float)salary;
- (void)PrintEmployee;
- (void)dealloc;
@end

Here's Employee.m:

#import "Employee.h"
@implementation Employee
    - (id)initWithName:(char *)name andID:(long)id
                            andSalary:(float)salary {
        strcpy( employeeName, name );
        employeeID = id;
        employeeSalary = salary;
        
        printf( "Creating employee #%ld\n", employeeID );
        
        return self;
    }
    
    - (void)PrintEmployee {
        printf( "----\n" );
        printf( "Name:   %s\n", employeeName );
        printf( "ID:     %ld\n", employeeID );
        printf( "Salary: %5.2f\n", employeeSalary );
        printf( "----\n" );
    }
    - (void)dealloc {
        
        printf( "Deleting employee #%ld\n", employeeID );
        
        [super dealloc];
    }
@end

and here's main.m:

#import <Foundation/Foundation.h>
#import "Employee.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool =
                        [[NSAutoreleasePool alloc] init];
    Employee*   employee1 = [[Employee alloc] initWithName:"Frank Zappa" andID:1 andSalary:200];
    Employee*   employee2 = [[Employee alloc] initWithName:"Donald Fagen" andID:2 andSalary:300];
    [employee1 PrintEmployee];
    [employee2 PrintEmployee];
    
    [employee1 release];
    [employee2 release];
    
    [pool release];
    return 0;
}

As you did before, run the project. This time, compare your results with Figure 2. Look familiar? The Salary field looks a bit different but, other than that, the results are identical.


Figure 2. The results of the Objective C version of Employee.

Till Next Month...

Take some time and look at the Objective-C and C++ code side-by-side. You should be able to tell a lot about the Objective-C code just from this context. In next month's column, we're going to dissect the Objective-C code, line-by-line. Till then, here's some food for thought:

In C++ you call a member function. In Objective-C you send a message to a receiver. For example, in C++ you might use an object pointer to say:

myObj->changeValue;

In Objective-C you'd say:

[myObj changeValue];

Objective-C classes are, like their C++ counterparts, broken into an interface (the ".h" file) and an implementation (the ".m" file). Instance methods start with a "-" character in the interface, class methods start with a "+".The type "id" is Objective C's generic object pointer type. You'll see it a lot.

(id)myFunc; declares a function named myFunc that returns an id pointer.

(id)getP:(int)p; declares a function named getP: that takes a parameter p of type int. The colon at the end of the function name indicates that the function takes a parameter.

(id)getP:(int)p andQ:(long)q declares a function named getP:andQ: taking two params - an int named p and a long named q.

Look through the code, think about what's happening, see you back here next month...


Dave Mark is very old. He's been hanging around with Apple since before there was electricity and has written a number of books on Macintosh development, including Learn C on the Macintosh, Learn C++ on the Macintosh, and The Macintosh Programming Primer series. Check out Dave's web site at http://www.spiderworks.com

 
AAPL
$567.77
Apple Inc.
+43.02
MSFT
$39.86
Microsoft Corpora
+0.17
GOOG
$525.16
Google Inc.
-1.78

MacTech Search:
Community Search:

Software Updates via MacUpdate

Ember 1.5.1 - Versatile digital scrapboo...
Ember (formerly LittleSnapper) is your digital scrapbook of things that inspire you: websites, photos, apps or other things. Just drag in images that you want to keep, organize them into relevant... Read more
Cyberduck 4.4.4 - FTP and SFTP browser....
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
TechTool Pro 7.0.3 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
MacFamilyTree 7.1.6 - 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
EtreCheck 1.9.9 - For troubleshooting yo...
EtreCheck is a simple little app to display the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support... Read more
TeamViewer 9.0.28116 - Establish remote...
TeamViewer gives you remote control of any computer or Mac over the Internet within seconds, or can be used for online meetings. Find out why more than 200 million users trust TeamViewer! Free for... Read more
DiskAid 6.6.3 - Use your iOS device as a...
DiskAid is the ultimate transfer tool for accessing the iPod, iPhone or iPad directly from the desktop. Access data such as: music, video, photos, contacts, notes, call history, text messages (SMS),... Read more
Viber 4.1.0 - Send messages and make cal...
Viber lets you send free messages and make free calls to other Viber users, on any device and network, in any country! Viber syncs your contacts, messages and call history with your mobile device,... Read more
Apple iOS 7.1.1 - The latest version of...
The latest version of iOS can be downloaded through iTunes. Apple iOS 7 brings an all-new design and all-new features. Simplicity Simplicity is often equated with minimalism. Yet true simplicity is... Read more
1Password 4.3 - Powerful password manage...
1Password is a password manager that uniquely brings you both security and convenience. It is the only program that provides anti-phishing protection and goes beyond password management by adding Web... Read more

Latest Forum Discussions

See All

Pen & Ink Review
Pen & Ink Review By Jennifer Allen on April 24th, 2014 Our Rating: :: CONVENIENT ARTISTRYiPad Only App - Designed for the iPad Pen & Ink is an ideal way to sketch down some visual ideas or simply spark your imagination.   | Read more »
Sonic & All-Stars Racing Transformed...
Sonic & All-Stars Racing Transformed Now Free, to Add New Iconic Characters and Elements Posted by Tre Lawrence on April 24th, 2014 [ | Read more »
Soccer Rally 2 Review
Soccer Rally 2 Review By Carter Dotson on April 24th, 2014 Our Rating: :: GOALKEEPINGUniversal App - Designed for iPhone and iPad Soccer Rally 2 is the most serious vehicular soccer game ever created.   | Read more »
Galaxy Conquerors Review
Galaxy Conquerors Review By Jennifer Allen on April 24th, 2014 Our Rating: :: RETRO SHOOTINGUniversal App - Designed for iPhone and iPad Old school shooting is fun but inaccurate in Galaxy Conquerors.   | Read more »
Yomi Review
Yomi Review By Rob Thomas on April 24th, 2014 Our Rating: :: C-C-C-COMBO BREAKERiPad Only App - Designed for the iPad Round One – Fight! No quarters required for this iOS adaptation of a tabletop adaptation of the arcade fighting... | Read more »
Injustice: Gods Among Us Updated with Ne...
Injustice: Gods Among Us Updated with New Characters, Leaderboards, Gear, and Online Multiplayer Posted by Rob Rich on April 24th, 2014 [ | Read more »
Spin It Review
Spin It Review By Jordan Minor on April 24th, 2014 Our Rating: :: SPIN ME RIGHT ROUNDUniversal App - Designed for iPhone and iPad Spin It has a fine puzzle game model, but its execution lacks energy.   | Read more »
Productivity App NoteSuite is Having its...
Productivity App NoteSuite is Having its Biggest Sale Ever, Just for One Week Posted by Rob Rich on April 24th, 2014 [ permalink ] | Read more »
Wayward Souls Review
Wayward Souls Review By Carter Dotson on April 24th, 2014 Our Rating: :: CARRY ON, WAYWARD SONUniversal App - Designed for iPhone and iPad Wayward Souls is a roguelike-inspired action-RPG that sets a high bar for other games to... | Read more »
The Sandbox Gets Update, Receives New Ca...
The Sandbox Gets Update, Receives New Campaign and New Elements Posted by Tre Lawrence on April 24th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »

Price Scanner via MacPrices.net

Strong iPhone Sales Drive Apple Record March...
Apple on Wednesday announced financial results for its fiscal 2014 second quarter ended March 29, 2014. The Company posted quarterly revenue of $45.6 billion and quarterly net profit of $10.2 billion... Read more
Award-Winning NoteSuite Productivity App is $...
Minneapolis based Theory.io has announced an 80-Percent Markdown NoteSuite for iPad. NoteSuite helps users stay organized by capturing their notes, to-dos and documents in one organized place.... Read more
16GB 1st generation iPad mini available for $...
Radio Shack has a select number of refurbished 1st generation 16GB WiFi iPad minis available for $199.99 on their online store. Choose free shipping or free ship-to-store. We expect these to sell out... Read more
13-inch 2.5GHz MacBook Pro on sale for $100 o...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for $1099 including free shipping plus NY sales tax only. Their price is $100 off MSRP. Read more
iPad Sales “Lull” A Reality Correction Of Unm...
I have lots of time for Jean-Louis Gassée, the former Apple Computer executive (1981 to 1990) who succeeded Steve Jobs as head of Macintosh development when the latter was dismissed in 1985. Mr.... Read more
Apple Makes OS X Betas Available To All – Wit...
Apple’s OS X Beta Seed Program, which lets you install the latest pre-release builds, try it out, and submit your feedback, is now open to anyone who wants to sign on rather than to developers and... Read more
Apple Releases iOS 7.1.1 Update
The latest iOS 7.1.1 update contains improvements, bug fixes and security updates, including: • Further improvements to Touch ID fingerprint recognition • Fixes a bug that could impact keyboard... Read more
Logitech Announces Thinner, Lighter, More Fle...
Logitech has announced an update to its Ultrathin for iPad Air, iPad mini and iPad mini with Retina display, improving the flexibility and design of its award-winning predecessor with an even thinner... Read more
Logitech Introduces Hinge, Big Bang and Turna...
Logitech has announced expansion of its tablet product line with three new cases – the Logitech Hinge, the Logitech Big Bang and the Logitech Turnaround – each for the iPad Air, iPad mini and iPad... Read more
WaterField’s Rough Rider Leather Messenger Ba...
WaterField Designs have announced the new 15-inch size of their popular Rough Rider leather messenger bag, a vintage-looking bag that combines Old West charm and ruggedness with distinctly modern... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Position Opening at *Apple* - Apple (United...
…customers purchase our products, you're the one who helps them get more out of their new Apple technology. Your day in the Apple Store is filled with a range of Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Inc. Research Data Specialist - Appl...
…of Worldwide Market Research & Intelligence. The team is responsible for conducting Apple branded consumer market research. It is also responsible for analyzing data Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.