Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
A more formal way of saying it is that the square brackets are used to denote a messaging expression of the form [receiver message]. The receiver is always either an object or a class (in the case of static methods) and the message is a particular method that you want the object to invoke and its associated parameters.
 
Thanks for your explanation.

I am reading "Programming in Objective-C 2.0", I found there are a lot of new type syntax, not C neither C++, or Java.

I just want to know why they don't use general syntax if you know the reasons.

I think pool.drain or pool->drain are easy to understand, maybe pool+>drain is better if they want to the newest syntax:(
 
Thanks for your explanation.

I am reading "Programming in Objective-C 2.0", I found there are a lot of new type syntax, not C neither C++, or Java.

I just want to know why they don't use general syntax if you know the reasons.

Most likely so that you (and the compiler) can tell which parts of the code are Objective-C. You get used to it fairly quickly.
 
Whenever you see square brackets used like [pool drain] you're seeing an Objective-C message. Sometimes you'll see it used with methods that take parameters:

[NSString stringWithString: someString];

even methods that take multiple parameters:

[someObject setValue: aValue forKey: aKey]; in this example "setValue:forKey:" is considered one method (or verb) even though it's split up into multiple parts.

The way I like to think of messaging is [subject verb] or [subject verb: informationTheVerbNeedsToDoItsTask].

Each object can only respond to actions that it knows about so if you hypothetically had a dog object you could pass the message [dog sit], [dog barkForNumberOfSeconds: 15], [dog chaseBurglar: theFatBurglar biteBurglarIfCaught: YES]. But you couldn't pass messages like [dog layEgg] or [dog driveToMall].

The only other time you'll run across square brackets is when you're dealing with old-school c arrays. In this context, the brackets are denoting which item out of an ordered collection of things you're talking about so if you had an array of starWarsFilms[3] would refer to episode 4 (because arrays start with 0).

When you're working with Objective-C you'll typically be using NSArray and NSMutable Array (which are objects that use the same messaging syntax that all other objects use), so you won't see many c arrays. I hope that didn't confuse you more lol.
 
The Objective-C message passing syntax is based on and almost identical to Smalltalk's. Basically Obj-C just adds the square brackets to Smalltalk's syntax to clarify things a bit.
 
I thought only [pool drain] can be used in objC, but what different from code as below, I also want to know if I can use pool.drain?

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

myFraction.numerator = 1;
myFraction.denominator = 3;
 
One more thing, I think it is easy to understand if insert a comma in below code
[myFraction setNumerator: 1, andDenominator: 3];

Do you think so?
 
I also want to know if I can use pool.drain?

pool.drain is wrong! You can use dot-syntax to call an object's accessors to it's properties, but you can't use dot-syntax to pass any other type of message. You'll learn to love the square brackets because it let's you quickly/easily recognize Objective-C messages from the rest of the code.
 
This confusion is one of the reasons I do not like the . syntax for accessors. It only stands to muddle things up. Dots are for local structs. [] is for passing messages.

Also starWarsFilms[3] would be past the end of the array, there are only 3 actual Star Wars films ;) (jk, episode 3 wasn't that bad).

-Lee
 
Thanks for your explanation.

I am reading "Programming in Objective-C 2.0", I found there are a lot of new type syntax, not C neither C++, or Java.

I just want to know why they don't use general syntax if you know the reasons.

I think pool.drain or pool->drain are easy to understand, maybe pool+>drain is better if they want to the newest syntax:(


Java/C/C++ isn't "general syntax" it's Java/C/C++ syntax. Objective-C is older than Java, and the same age as C++, so neither of those were around to copy.
 
Also starWarsFilms[3] would be past the end of the array, there are only 3 actual Star Wars films ;) (jk, episode 3 wasn't that bad).

-Lee

Technically, that is not quite correct. Yes, there are only 3 Star Wars films, but the array index must be adjusted to starWarsFilms[episode - 4] to correctly index the array. The later releases fell into a universe long long ago and far far away*, hence the negative indexes (that match most reviews).

*Not to be confused with Princess Fiona's hometown.
 
Java/C/C++ isn't "general syntax" it's Java/C/C++ syntax. Objective-C is older than Java, and the same age as C++, so neither of those were around to copy.

And as I mentioned, Objective-C borrows a lot of its messaging syntax from Smalltalk, which is older than both C++ and Java, and about the same age as C.
 
Thanks for your explanation.

I am reading "Programming in Objective-C 2.0", I found there are a lot of new type syntax, not C neither C++, or Java.

I just want to know why they don't use general syntax if you know the reasons.

I think pool.drain or pool->drain are easy to understand, maybe pool+>drain is better if they want to the newest syntax:(

The creators of the Objective C language disagree. And I suppose they have more experience than you do. And now consider what syntax you would use for methods with parameters, and with multiple parameters.
 
Technically, that is not quite correct. Yes, there are only 3 Star Wars films, but the array index must be adjusted to starWarsFilms[episode - 4] to correctly index the array. The later releases fell into a universe long long ago and far far away*, hence the negative indexes (that match most reviews).

*Not to be confused with Princess Fiona's hometown.

This gets pretty tricky, then.
Code:
char *allThingsLucasCallsStarWars[6] = {"Episode I: The Phantom Menace","Episode II: The Clone Wars", "Episode III: Revenge of the Sith", "Episode IV: A New Hope", "Episode V: The Empire Strikes Back", "Episode VI: Return of the Jedi"};
char **starWarsFilms = &allThingsLucasCallsStarWars[3];
printf("The one with the most Jar-jar: %s\nThe first one: %s\n" starWarsFilms[-3],starWarsFilms[0]);

I can mark this as the first time i've intentionally written code to access negative array indicies and have it work.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.