3

I'm learning Objective C (from a C# background) and am having difficulty understanding the unusual method signatures and am looking for a shortcut to make the learning curve less steep.

  • When reading Objective C code, what English words or phrases can I associate with the symbols?

For example, calling a method looks like this:

  @implementation Calculator

  -(int) add:(int)a and:(int)b {
  }
  @end

I would write this code:

Calculator *calculator = [[Calculator alloc] init]];
int sum = [calculator add:10 and:20];

So far in my learning cheat sheet I wrote: "[" means "do" . What other word associations are there for symbols in Objective C?

  • ObjC isn't the most fluent expressive language there is...trying to assign meaning explicitly might be a mistake. – Rig Aug 21 '13 at 23:56
2

So far in my learning cheat sheet I wrote: "[" means "do"

I wouldn't think of it that way. Instead, try to remember that a method call looks like this:

[target message]

The brackets enclose a pair where the first item is the object that you're sending the message to, and the second is the message that you're sending. It doesn't take any longer to get used to brackets used like that than to learn that {and } enclose a group of statements in C-like languages. If you try to translate the brackets to some other meaning you might just make the whole idea more complicated. If you need a way to "say" the brackets as you read them, think of them as "start method call" and "end method call".

As for the messages themselves, it may help to remember that the name of a method includes all its parts. In your example, the method is:

add:and:

I know it's surprising that the parameters are inserted between parts of the method name, but once you get past the surprise you'll find that it's very nice to use the method name to remind you of which parameter is which.

|improve this answer|||||
2

I wouldn't try to equate it to anything in English. Without reading the brackets you can read it in plain English: Calculator add 10 and 20. [] are used for containment/grouping.


Basic Example:

I would first start by translating the syntax to a programming language you already know.

And I would first start by learning an example without named parameters:

[calculator reset]; means: calculator.reset();


Nesting:

[[container calculatorInstance] reset]]; means container.calculatorInstance().reset();

So the first item after the [ is an object (or class). After the space is a method being called on the object (or class).

So a set of brakets [] could be thought of as object.method() and if you were to take that value and call a method on it you would have an outer set of [].

[[something returnAValue] actOnThatValue];

is the same as

something.returnAValue().actOnThatValue();
|improve this answer|||||
1

Not particularly fluent in Objective-C, but [calculator add:10 and:20]; means as much as "send a message to calculator to add 10 and 20".
From what I remember Objective-C emphasizes that a method call is actually sending a message to an object, sometimes with a payload (the arguments to a method call as it were), which is a bit different than how you would think about it in C#.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.