What you want to understand is the method declaration syntax. In Objective-C, a method has a name which looks like this doSomethingWith:andAlso:inMode:
. Each colon precedes an argument. When you declare or define a method, you also specify the types of the variables and their names; the type is given in parentheses. You also prepend things with a -
for instance methods and a +
for static methods, as well as the (parenthesized) return type. Now, you have
- (NSString*) pickerView:(UIPickerView*)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
...
}
When we decipher this, we find that:
-
: It's an instance method.
(NSString*)
: The return type, NSString*
.
pickerView:
: The first part of the method name.
(UIPickerView*)pickerView
: The first argument; its name is pickerView
and it has type UIPickerView*
.
titleForRow:
: The second part of the method name.
(NSInteger)row
: The second argument; its name is row
and its type is NSInteger
.
forComponent:
: The third part of the method name.
(NSInteger)component
: The third argument; its name is component
and its type is NSInteger
.
Thus, putting it all together, this defines the instance method pickerView:titleForRow:forComponent:
; it returns an NSString*
, and the three arguments it takes are of type UIPickerView*
, NSInteger
, and NSInteger
, respectively. This method would be called as [obj pickerView:myPV titleForRow:myRow forComponent:myComponent]
.
And just for further reference: in isolation, if you have NSString* str
, it declares a variable of type NSString*
; and if you have (NSString*)obj
, it (forcefully) converts that object to have type NSString*
. This has no connection to the method declaration syntax.
Edit 1: I also saw that you were asking about the asterisks. In (Objective-)C, this represents a pointer. If you have an int x
, say, then when you write int y = x
and then y = 3
, the original value of x
is unchanged. If, however, you have an int* px
, then you can write px = &x
. (Note that ints, declared as int
, are a completely different data type than int pointers declared as int*
. Writing int y = &x
is garbage, as is int* py = x
, and so on.) This &
is the "address of" operator; it finds where x
is in memory and returns it. Now, if you write int* py = px
and then py = &y
, this won't change px
. But if you write *px
, you access the value currently stored in x
, and you can change it: *px = 42
sets x
to 42
. For various reasons, when working with objects, people tend to like to work with references to them instead of their actual values; thus, in Objective-C, you only handle objects through pointers. What this means is that you will never see NSMutableArray x
, only NSMutableArray* x
; and that if you have NSMutableArray* y = x
, then x
and y
are, roughly speaking, the same, and calling [x addObject:obj]
affects y
as well. There are more comprehensive tutorials out there—it's worth checking them out if you don't understand pointers—but this should suffice as an overview.
Edit 2: In another comment, you say you're coming from Ruby and Python. In Ruby (and I think Python, but I've used it less), every variable is a reference. This means that the basic use of pointers for object types should be familiar; as long as you never use &
or *
, they'll function in pretty much the same way. The difference between pointers and references is that you can take references to objects and create pointers to pointers. For instance, many methods end in ...error:(NSDictionary**)error
. This effectively provides an extra return value; in the method, if something goes wrong, they can write *error = myErrorInfo
. Since function arguments are copied, error = myErrorInfo
wouldn't be visible; however, the pointer's referent is still the same, and so it can be assigned to. If you then write code such as:
NSDictionary* errorDict = nil;
[obj unsafeOperation:@"http://stackoverflow.com" error:&errorDict];
You pass in a pointer to errorDict
so that the unsafeOperation:error:
method can write to errorDict
and you can see it.