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

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
22
0
Curious to know, if there is any logic behind the syntactical structure, where 1st argument is not labelled, but following arguments only need labelling ?


Code:
[myObject myFunction:firstArgument theSecondArgument:secondArgument];


Or is it just how it is made. There is no logic. ?
 

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
Typically if you were to create a method signature it would be something that would label the first argument itself. This can be seen with almost any of apples method signatures.

Code:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCELL_IDENTIFIER];

We can see that the first argument is prefixed with with initWithStyle thus labeling the first argument as a style and telling us that the function call is an init function.

Another such example in MKMapKit, imagine _mapView is an MKMapView reference.

Code:
[_mapView convertCoordinate:coordinate toPointToView:self.view];

We are converting a coordinate to a specific view that is provided by the first argument.

The logic behind the naming is to be descriptive that is the nature of Objective-C.
 

Vishwas Gagrani

macrumors newbie
Original poster
Sep 26, 2012
22
0
ok, thnx for explanation.

However, when i think something like this :
Code:
[ mathObj addTheNumbers:number0  number1:number1 ]

it looks to me awkward. But may be it's just a matter of practice. :)
 

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
Shouldn't that be a class method instead of an instance method?

Honestly something as trivial as adding numbers should be a function not even a method of a class or object. Whether it be a static or instance class.

In that case it would be:
Code:
int add(int x, int y)
{
     return x + y;
}

With usage of:
Code:
int result;
result = add(10, 20);

I think the original poster was simply asking about naming convention used for objective-c methods regardless or static or instance.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.