Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Simmons

macrumors newbie
Original poster
Dec 28, 2009
20
0
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?
 
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.
 
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.
 
I Believe you can actually use '(NSString) *mystring' or '(NSString *) mystring' equally the same.

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).
 
Why does the asterisk go IN the parenthesis?

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.
 
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).

Thanks for the correction, I wasn't 100% sure. Edited my comment.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.