Tip-toeing in Objective-C: A first look at the syntax and seeing its true beauty.

Posted on April 4, 2008
Filed Under Cocoa | 4 Comments

I’m beginning to learn Objective-C in my spare time and its syntax is something that’s taking me surprisingly long to get used to. I understand the concepts sufficiently well and they map to ones that you’ll find in most other languages. But, again, the syntax is… different.

I’ve never written any Smalltalk before, which is a language with a syntax that Objective-C was heavily influenced by, but I have written plenty of Java and now spend most of my time writing C# in .NETland. The Cocoa I mentioned in the title of this article is essentially to Objective-C what .NET is to C#, a bunch of free goodies. I’m sure I’ll get into that in a future post, but for now, I want to talk about the basics (being that that is all I know at the moment).

Objective-C is an object oriented language like PHP, C#, Java, etc so passing around chunks of data along with their functionality is very easy. So, let’s start from the top.

Let’s say we have a class. We’ll call it Dog. All dogs can bark, they have a name, they have a hair color, and maybe even a personality. We can encapsulate all of this information about how we describe a dog and what a dog can do in a class.

In PHP, the dog class would look something like this:

class Dog {
  var name;
  var personality;
  var hairColor; 

  __construct($name) {}

  public function bark($volume) {
  if($volume == "loud")
       echo "'WOOF!,' said " . $this->name;
  else
  echo "'Yip,' said " . $this->name;
  }
}

To keep things super simple, I left out privatization of variables and any mutators. If you don’t know what those are, it’s not important at all yet, so don’t worry. The important thing is that you understand that all the information about a dog is stored in this thing we’re going to call a class.

Here’s some simple code that uses it:

 $myDog = new Dog("Zoey");

 $myDog->bark("loud");    // prints out "'WOOF!,' said Zoey.";

First, I constructed an instance of the Dog class creating a Dog object referenced by the variable $myDog. I can then set the member variable, $name, using the ->. After setting the properties of the Dog object, you can then retrieve them. You can also make Zoey bark by telling her to with $myDog->bark(). It’s that easy.

Now, you have entered the twilight zone…… doo do doo do… yeah, well. Now, we’ll do the same thing in Cocoa and compare them. In Objective-C, like C++ and other languages, a class is made up of two files: an interface (.h) and an implementation (.m).

This is the interface:

@interface Dog : NSObject
{
  NSString name;
  NSString personality;
  NSString hairColor;
}
- (void)barkAtThisVolume:(NSString)volume;
@end

This is the implementation:

@implementation Dog
- (id)initWithName:(NSString)dogName {
   name = dogName;
}

 

- (void)barkAtThisVolume:(NSString)volume {
  if(volume.equals(@"loud"))
    Console.Log("'WOOF!,' said " + name);
  else
    Console.Log("'Yip,' said " + name);
}
@end

To get things started, I must mention the obvious; PHP is a very simple language and has many limitations. There are no object libraries written by the people who make PHP. There are third party ones like Pear, et al, but nothing written by the lovely people at PHP.net. Maybe that’ll be coming in the future though, because even classes in general were not supported until just a few short years ago. That said, you will notice things in Objective-C that you simply wont find in PHP. Granted, I am referring to things in the Cocoa Framework, which like I said before, is to Objective-C what .NET is to C# (or Pear is to PHP). The difference between Cocoa or C# and Pear is that Pear is written by a third party and Cocoa is written by the primary contributor/creator of Objective-C, Apple. Same goes for C# and .NET, whose creator is the gigantor – Microsoft. The point that I’m getting at is you probably read NSObject and thought, wtf?

NSObject is the root class of most Objective-C class hierarchies. Basically, by inheriting NSObject, you get the core functionality that you should expect when using the Cocoa framework. So, after all that, we sort of didn’t need NSObject, but now I wrote that last paragraph and I’m not about to go and delete it.

The interface is where you define your member variables and your method signatures. The implementation is where you actually use it all and do the heavy lifting. Here is the same code we used above in PHP, in Objective-C:

Dog myDog = [[Dog alloc] initWithName:@"Zoey"];

[myDog barkAtThisVolume:@"loud"];

Constructing a PHP object happens with the new operator. In Objective-C, this happens with alloc. In PHP, we create a function called __construct and call it using the name of the class, Dog( ). In Objective-C, we call a function conventionally prefixed with init. Now, this next part is where Objective-C really shines. I’m not sure if you noticed it right away, so let’s put the PHP call to bark next to Objective-C’s.

PHP
$myDog->bark("loud");

Objective-C
[myDog barkAtThisVolume:@"loud"];

What do you not know when you read the PHP code, that you know right away when reading the Objective-C? You know exactly what “loud” means. There’s no question because Objective-C has named parameters. A named parameter puts exactly what a variable is supposed to be about directly in the signature. Let’s look at something a little more complicated and you’ll really start to know the difference. I’m not going to define the classes, but will expect you to imagine what they would look like.

Imagine if our dog class had a method that took your dog for a walk. I want to make this overly complicated to show a point, so it will take in parameters for different things. I’m not going to tell you what they are… again to prove a point, so read the code below, think about it really hard, and then read on.

PHP
$myDog->walk("20", "30", true);

 

Objective-C
[myDog walkDogaDistanceInMilesOf:@"20" OrForADurationInMinutes:@"30" StopToPlayAtThePark:YES];

First off, give me a break if you don’t like my parameter names. I think it goes to show what I mean, though. In PHP, you have no clue what the parameters mean unless you comment your code. Objective-C is almost completely self-documenting. Each line of code completely explains what should come in and what should be expected as output. While it definitely helps when you create human-readable variables and comment where necessary, Objective-C makes this a whole lot easier to do.

After I learn more of the Cocoa Framework, I’ll be writing again with more useful code. I suppose our Dog class will have to get you by until then.

[Update] I honestly forgot to post what the parameters were after I wrote the Cocoa! Maybe that means, we should start documenting PHP, C#, etc with Objective-C! How lovely.

Comments

4 Responses to “Tip-toeing in Objective-C: A first look at the syntax and seeing its true beauty.”

  1. Thomas on August 1st, 2009 3:43 pm

    Thank you for this, it is starting to slowly sink in…

    I think learning PHP first put me at a disadvantage because it makes you think in a very linear and shallow manner.

  2. Florence Haseltine on November 18th, 2009 10:20 am

    We do need a comparison of not only coding (scripting) but also concepts. I know the languages are different but getting the work done is important.

    PHP include_once(’Api.php’);
    Objective- C #import

  3. Andrew on December 19th, 2009 9:54 pm

    I don’t think PHP was the best language for comparison as it’s not an Object-Oriented language. It’s a procedure language that supports classes, kinda of like a hybrid language.

  4. kris on January 27th, 2010 8:01 pm

    What’s been hard for me to get my head around in Objective-C is the pointer (*)

    I do procedural PHP so the idea reference and pointer is still something I’m trying to understand.

    I’m used to just declaring a variable and use it.

Leave a Reply




Captcha
← Enter these letters.