Question

Well, I've started with iPod/iPhone programming using Head First iPhone Development (O'reilly) and I'm typing code out of the book. There are two problems, one is programming related and the other is not.

  • I don't understand the format of objective-c methods. I'm getting an few errors now, based on source code from the book. Which leads me to my next issue.

  • Some of the code is buggy. I think so because I couldn't get the code to run without modifying it. The book has some typos in the text since it's a first edition and whatnot, but could my "fixing" the code have to do with it?

So... Where can I learn more about objective-c methods and how they work in terms of structure and where the return type and arguments go?

For those with the book, I'm in the middle of the InstaTweet app towards the beginning.

Thanks.

Was it helpful?

Solution

Maybe a comparison between a "c-like" language and obj-c would be useful -- let's go with Java. Imagine a Rectangle class with a setBackgroundColor method. We'll assume we have an instance of Rectangle called rect.

In Java, the method signature would likely be

public void setBackgroundColor(int r, int g, int b) { ... }

In Objective-C, arguments are part of the method signature, so it might be

- (void)setBackgroundColorWithRed:(int)r green:(int)g blue:(int)b;

The "-" means it's an instance method on the Rectangle class. (void) is the method's return type. Then come the arguments. Each colon defines an argument, which is typed (each arg is (int) in this example).

Let's compare calling these methods. Java:

rect.setBackgroundColor(255, 255, 0);

Obj-c:

[rect setBackgroundColorWithRed:255 green:255 blue:0];

A lot of people stumble on obj-c's syntax -- you're not alone. Hopefully this comparison will make things clearer. It also exemplifies a linguistic strength of objective-c: at call time, it's clear what your arguments are. If I were reading code and saw the Java method being called, it wouldn't be obvious that the arguments are red, blue, and green. In obj-c, it's painfully clear that we're setting individual color values. Granted, most developers can guess what the three arguments to a setColor method are, but more complex methods get confusing. Here is a more complex method defined in Java and objective-c:

static Dog createDog(  String name
                     , int age
                     , boolean isNeutered
                     , String ownerName
                    );
+ (Dog *)createDogNamed:(NSString *)name 
     age:     (int)        age 
     neutered:(BOOL)       isNeutered 
     owner:   (NSString *) owner;

At call time:

Dog.createDog("Fluffy", 2, true, "Moshe");
[Dog createDogNamed:@"Fluffy" age:2 neutered:YES owner:@"Moshe"];

Objective-c is more verbose, but much more readable. In the Java example, it's not really clear what the "2" and "true" arguments mean. Objective-c almost reads like English.

Other people have posted links to more in-depth guides, but I hope this gets you past the syntactic stumbling blocks enough to make the links useful. I'm happy to answer more specific questions you have. In my experience, obj-c newbies have a really tough time with syntax, and then it clicks and feels brilliant. Hang in there!

OTHER TIPS

The definition of an Objective-C method is pretty straightforward. It's pretty similar to a regular C function call definition, actually. The big addition is the option of naming the parameters. Here's an example:

-(int)addX:(int)x toY:(int)y
{
    return x + y;
}

Which you can use by sending a message to the appropriate object:

int z = [object addX:x toY:y];

In this case I've given the parameters and the arguments the same names, to make it clear how things line up. You could just as easily do:

int z = [object addX:4 toY:7];

And after that, z will be 11.

Check out Apple's Intro to Objective C.

The basic method format is

-(ReturnType *)methodName:(VariableType *)variableName{

}

For example:

-(NSString *)giveMeANewStringFromAnOldString:(NSString *)oldString{

}

You should also check the book's website for the errata.

I did the same thing you did. I picked up the Head First iPhone Development book too (covering 3.1 SDK).

I ran through all the tutorials in it and found only 1 error that caused my application to not work (I made a note of it in my book but I can't find it now...).

So after that I realized I had exactly the same problem you are having with the syntax.

I picked up "Programming in Objective-C 2.0" by Stephen G. Kochan. (It is simply brilliant.) The book is clear and concise. I didn't even make it through 25% of the book and all my questions were answered.

Reading objective-C code threw me for months because I'm so used to a different syntax that my brain took a little while to adjust to the way objective-C expects.

What I'm finding is Objective-C code becomes really readable once you wrap your brain around it. Carl Norum's example above is a great demonstration of readability.

[object addX:4 toY:7];

It doesn't get much more readable than that!

Also some terminology is a bit different. "Messaging an object" vs "calling a method" threw me for a few weeks too.

I hope the book recommendation helps you, it really helped me a LOT.

There are some great learning resources from Apple:

Learning Objective-C: A Primer

and

Coding Guidelines for Cocoa

Good Luck!

Read Cocoa(R) Programming for Mac(R) OS X (3rd Edition), it's the bible for Mac and Cocoa.

I haven't had any issues with it but keep in mind that API are always moving forward so there might be slight updates/changes. But I haven't found anything that would stop me from finishing all the code examples and exercises.

This is THE book to read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top