TweetFollow Us on Twitter

May 97 Getting Started

Volume Number: 13 (1997)
Issue Number: 5
Column Tag: Getting Started

More Objective-C

by Dave Mark, ©1997, All Rights Reserved. http://www.spiderworks.com

Last month, we took our first taste of Objective-C. We learned that Objective-C sources use the extension ".m" for main source files and ".h" for header files. The type "id" is used to declare a generic pointer which allows us to delay type binding decisions. We learned that most objects are derived from the Root class Object. The Object class features variables and methods inherited by all other classes. One of these variables is "isa" which specifies the class to which an object belongs.

We saw the form for a class interface:

 MySuperClass
{
 instance variable declarations
}
method declarations

We also learned about Objective-C's funky form of method declaration:

- (int)getX:(int)x andY:(int)y;

Where the leading minus sign marks the function as an instance method - as opposed to a "+", which marks a method as a class method, like a C++ static method. The first "int" is the return type, and the other "int"s are the parameter types. The name of the method above is "getX:andY:" The default type is id, so if you leave out any of the types (including the return type) the type is set to id.

We also learned about the #import compiler directive, used to avoid multiple inclusion of a .h file:

#import "Object.h"

Alternatively, you can avoid all the extra junk you get when you import a classes' header file by using the @class directive to let the compiler know that a reference is to a legal class:


Finally, we saw the form used for the actual implementation of a class:

#import "MyClass.h"

Message Receivers and Message Syntax

Now it's on to new material. Once you declare your class and use that declaration to define an object, it's time to bring that object to life by sending it a message. In C++, you call an object's member function using the object itself to make the call:

myObjectPtr->MemberFunction();

In Objective-C, a message is said to be sent to a receiver using the following syntax:

[receiver message];

The receiver is an object and the message is a method, along with its associated parameters. The message is sent (and the appropriate method selected) at runtime. For example, suppose the class named MyClass included the following method:

- (int)getX:(int)x andY:(int)y;

Just as a reminder, this method is called getX:andY:, takes two parameters (both of them ints), and returns an int. Here's a line of code that sends a message asking a MyClass object to perform the getX:andY: method:

myNum = [myObj getX:27:50];

In this case, the object myObj (we'll see how to allocate an object in a second), presumably of class MyClass, gets sent the getX:andY: message, along with the parameters 27 and 50.

Allocating an Object

To allocate a new object, you'll send an alloc message to the class whose object you want to create. Here's an example:

id myObj = [[MyClass alloc] init];

You'll use this line of code again and again, with a few alterations of course. We start off by defining an id to hold the reference to the allocated object. The right side of the "=" operator is a message expression embedded in another method expression, much as you might embed a function call inside a second function call (printf( "%d", GetMyValue()); for example).

In this case, we are first sending the alloc message to MyClass. The alloc method is a factory method (it is declared with a leading "+" sign instead of a leading "-"), similar to a static member function in C++. It is inherited from the Object class. The alloc method allocates the memory for a single object of the specified class and returns a pointer to the object.

That object is then sent the init message, which causes that object's init method to be performed. A classes' init method should return a pointer to the object (otherwise this code wouldn't work). The object pointer returned by this nested pair of message expressions is assigned to the object pointer myObj.

As you would expect, Objective-C has an analogy to the C++ keyword "this". To refer to the current object, use the term "self". When you write your init method (to initialize your newly alloc'ed object), you'll end it by returning self:

return self;

Our First Objective-C Program

Let's bring all these concepts together with a simple example. We'll create a Number class that features a single variable, an int to hold the Number's current value. We'll add methods for initialization, one to square the Number's current value, and one to print out the current value. While this example may seem trivial, it will act as a nice syntax reference as you build your own classes.

By the time you read this, Metrowerks should have delivered their first Objective-C tools (scheduled for release as part of May's CW12). Since I don't have those tools in hand yet, I had to look at alternative Objective-C environments. As I mentioned last month, Tenon makes a Mac environment called CodeBuilder which supports the gcc Objective-C compiler. CodeBuilder supports an X environment called AfterStep, along with a more conventional unix terminal environment running either C Shell, T-C Shell (an improved C Shell), the traditional Bourne Shell (less powerful, but uses less memory), and the Bourne Again Shell (bash, Bourne Shell with improvements). All this rides on top of a Berkeley 4.4BSD unix system.

If you are into unix, CodeBuilder is definitely a lot of fun to play with. On the other hand, I find myself aching for the CodeWarrior IDE and editor to tie my projects together. Though I must admit that I did enjoy using vi to edit all my source code. Amazingly, I remembered all those cryptic vi commands that made vi such a pleasure/pain to use. Ah, well, enough reminiscing - on to the code...

The first thing I did was create a new folder to hold the source code (if you are using CodeBuilder, the unix command "mkdir dirName" creates a new directory, "cd new dir" changes directories, "mv oldname newname" changes file or directory names, "rmdir dir" deletes an empty directory, "vi filename" invokes the vi editor and "man command" brings up an online manual for the specified command. If you are new to unix or to vi, I would definitely try "man vi" to get a sense of the editor before you get into it).

Inside the new folder, I created three source files: Number.m, Number.h, and main.m. Here's the source for Number.m:

#import "Number.h"
//#include "Number.h"

(int)startValue
{
  [super init];

  value = startValue;

  return self;
}

- squareSelf
{
  value *= value;

  return self;
}

- print
{
  printf( "Number value: %d\n", value );

  return self;
}

The release of CodeBuilder I had did not support #import (at least not in the usual fashion). If your environment does not support #import, comment out that line and uncomment the #include. Remember, if you use #include, you'll need to also make a corresponding change to the header file (we'll get to it next).

Number.m contains the implementation of the Number class. All three methods are instance methods and are declared using the leading "-". Notice that the init method takes a single parameter, startValue, an int used to set the value of the instance variable named value. Value is declared in Number.h.

The init method starts off by sending an init method to its superclass (in this case, the Object class). Doing this gives your superclass a chance to initialize its superclasses and itself, a real good idea. After setting value to startValue, init returns a pointer to the newly initialized object (remember, self is like this in C++).

squareSelf and print are fairly straightforward. Note that both return self, though they don't necessarily need to, since their return value is ignored by main. Also, you can see that the C standard library function printf() is available in Objective-C. As Rhapsody starts to kick in, I'll start making use of Mac interface calls instead of using console i/o but, for now, we need to make do with what we have.

Here's the source for Number.h:

//#ifndef _Number_h_
//#define _Number_h_

#import <objc/Object.h>
//#include <objc/Object.h>


 Object
{
  int value;
}

- init:(int)startValue;
- squareSelf;
- print;

//#endif

Once again, if your environment doesn't support #import, uncomment all the commented code above and comment out the #import line. Note the declaration of the instance variable named value along with the declaration of the three methods init, squareSelf, and print. Note that all three return an id, since no return value is declared. That's why the:

return self;

at the end of each method makes sense.

Here's the source for main.m:

#include "Number.h"

void main()
{
  id number = [[Number alloc] init:27];

  [number print];

  [number squareSelf];
  [number print];

  [number free];
}

main() is just a sequence of method expressions. First, we declare number to be an object pointer, then we allocate and initialize a new Number object and assign the object's pointer (returned by init) to number. Note that the value 27 was passed in as a parameter to init. Got it?

The rest of the program is cake. Send number the print message. Here's the line of output that appears in the console window:

Number value: 27

Next, send number the squareSelf message. This causes the number object to square the value in value. Before you reach for your calculators, 27 * 27 = 729. Really.

To verify this, we pass another print message on to number. Here's the line that appears in the console window:

Number value: 729

Finally, we send the free message to number. Since the Number class does not override the Object classes' free method, the Object classes' free method is the one that ends up getting called free. The Object version of free deallocates all memory allocated for the object by alloc. Note that the Object version of free does not follow any pointers in your object to free any secondary memory. If you do allocate additional memory in your init method (don't override alloc), you'll want to override free and deallocate the additional memory.

If you do override free, be sure to send the free message to super:

[super free];

at the end of your free method to give super classes a chance to clean up after themselves.

By the way, if you are using CodeBuilder, you might want to take advantage of the unix tool called Make. Create a file called Makefile in the same directory as your sources, and type in these two lines:

Number: main.m Number.m Number.h
 gcc -o Number main.m Number.m -lobjc

Now you can recompile your program by just typing the command Make.

Till Next Month...

I am really enjoying the opportunity to mess with Objective-C. Though the syntax threw me at first, it didn't take me long before I was thinking in it. This is not a hard language. Just a bit weird! I'm definitely looking forward to getting Rhapsody up and running on my machine so I can experiment with true dynamic binding. Excellent!

Just a heads up - I have gotten a lot of email asking me to update my How to Get Started With Mac Programming article. So much has changed in the Mac universe (Rhapsody, the Internet, Objective-C, Java, etc.) that the old recommendations just don't hold water anymore. Within the next couple of months, I'm going to interrupt the Objective-C series (just for one month!) to run a new version of that article. For those of you who are already way Mac programmers, please bear with me. Till then, it's back to unix, vi, and Objective-C...

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.