(Something I don't get *)

Discussion in 'iOS Programming' started by Simmons, May 27, 2010.

  1. Simmons macrumors newbie

    Joined:
    Dec 28, 2009
    #1
    That title was supposed to be clever. The thing I don't get is....well...I can't really explain it. Here's a couple examples from an Apple example:

    - (void)presortElementNamesForInitialLetter: (NSString *)aKey;
    - (void)presortElementsWithPhysicalState: (NSString *)state;

    Can someone explain to me what those do? The main thing I am confused about is the whole ": (NSString *)aKey;" thing. Why does the asterisk go IN the parenthesis?
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    You need to understand C syntax, and the difference between an object and a pointer to an object.
     
  3. Hawkeye75 macrumors member

    Joined:
    Mar 29, 2010
    #3
    I'm still a little new myself so folks please correct me if I'm wrong.

    However what you need to understand is that you are telling the method that you are passing it an object of NSString type and that you want to call it mystring. You might have also seen '(id)sender'. id is a generic type and does not care what kind of object you are sending it. While 'sender' is again the name the method will use for the object. I've read that you should use the appropriate object type whenever possible instead of id but I don't know what the downfall of using 'id' is.

    As for the asterisk '*' you used that to indicate a pointer to said object. Just when you declare a new instance of an object.
    i.e. NSArray *myarray = [[NSArray alloc] init];

    Someone else might be able to explain the memory implications a little better but as I understand it an object can't be simply passed around like a 'primitive' data type like an integer. More importantly it makes sure there is only ever one unique instance of that particular object. So instead the application references the pointer to the object.
     
  4. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #4
    In C, the usage of pointers is extensively used. The asterisk is used to for declaring pointers.

    The asterisk (*) is used in 3 ways in C.
    1. It is used for multiplication: int a = 5 * 2;
    2. It is used in a declaration for a pointer:
      int a; // This is an integer
      int * b; // This is a pointer to an integer
      int ** c; // This is a pointer to a pointer to an integer.
      int *** d; // This is a pointer to a pointer to a pointer to an integer.
    3. It is used for dereferencing a pointer:
      int a = 5;
      int * b = &a; // The pointer b points to the address of a.

      *b = 2; // 'b' dereferenced (or the integer pointed to by b) is assigned a value of 2. 'a' will now be 2, 'b' still points to 'a'

    Master this and then you can complicate it with const and other storage specifiers.
     
  5. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    This is incorrect. Method return or parameter types must include all of the type information inside the (), so only (NSString *)mystring is correct. When you are declaring pointers in general in your code, you are free to use NSString *mystring or NSString* mystring. I prefer to use the former because it reminds me that in this case the * is associated with the variable being declared and not the type, so if I wanted to declare a bunch of NSString pointers, I would have to do NSString *one, *two, *three. NSString* one, two, three would only give me one pointers and two statically allocated variables (which is an error in Objective-C).
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #6
    Because when the language was designed, that's how they chose to do it.

    The types for method parameters and for method return-values are defined in the same way: in the manner of C type-casts. In C type-casts, all the type-names and all the modifying symbols like * and () must go inside one enclosing pair of parentheses.
     
  7. Hawkeye75 macrumors member

    Joined:
    Mar 29, 2010
    #7
    Thanks for the correction, I wasn't 100% sure. Edited my comment.
     

Share This Page