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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.