What is the logic of "no label" for 1st argument of a function

Discussion in 'iOS Programming' started by Vishwas Gagrani, Jan 7, 2013.

  1. Vishwas Gagrani macrumors newbie

    Joined:
    Sep 26, 2012
    #1
    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. ?
     
  2. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    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.
     
  3. Vishwas Gagrani thread starter macrumors newbie

    Joined:
    Sep 26, 2012
    #3
    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. :)
     
  4. phr0ze macrumors 6502a

    Joined:
    Jun 14, 2012
    Location:
    Columbia, MD
    #4
    Wouldn't it be better to pass that in another way?
     
  5. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #5
    If I were writing that method I would most likely name it some to this affect.

    Code:
    [mathObj add:sumNumber to:sumNumber];
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    Shouldn't that be a class method instead of an instance method?
     
  7. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #7
    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.
     

Share This Page