What does symbole [...] mean?

Discussion in 'Mac Programming' started by mikezang, Jun 2, 2010.

  1. mikezang macrumors 6502a

    mikezang

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I am studying Objective-C, I just want to know what does "[]" of [pool drain]; mean in short?
     
  2. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #2
    The square brackets are just Objective-C syntax used to apply methods to instances of a class. They dont necessarily mean anything.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    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.
     
  4. mikezang thread starter macrumors 6502a

    mikezang

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    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:(
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    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.
     
  6. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #6
    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.
     
  7. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #7
    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.
     
  8. mikezang thread starter macrumors 6502a

    mikezang

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #8
    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;
     
  9. mikezang thread starter macrumors 6502a

    mikezang

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    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?
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  11. mikezang thread starter macrumors 6502a

    mikezang

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    Thanks for your advise.

    I claimed because I just read them from today.
    I have to use because I want to develop app for iPad since today.
     
  12. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #12
    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.
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    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
     
  14. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #14

    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.
     
  15. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #15
    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.
     
  16. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #16
    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.
     
  17. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #17
    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.
     
  18. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #18
    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
     
  19. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #19
    lee++
     

Share This Page