Which is method name?

Discussion in 'iOS Programming' started by mikezang, Jul 7, 2010.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I know method in Objective-C as defined as below:
    Code:
    - (void) classMethod: (int) first secondArgument:(int) second;
    [B]-[/B]= Method type
    [B](void)[/B]= return type
    [B]classMethod[/B]= method name
    [B](int)[/B]= first argument type
    [B]first[/B]= first argument name
    [B]secondArgument:[/B]= second argument label
    [B](int)[/B]= second argument type
    [B]second[/B]= second argument
    But I am not sure what is the method name as below code, I thought method name should be numberOfRowsInSection and return type is (NSInteger), but I am in confuse about tableView:(UITableView *)tableView, what are they?
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of time zone names.
        return [timeZoneNames count];
    }
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The method name (as you would use in @selector for example) is tableView:numberOfRowsInSection:
     
  3. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Normally you wouldn't call it: it's a delegate method that will automatically be called by the UITableView. If you are struggling to understand this then I'm sorry but I don't have the time or energy to teach Objective-C basics. Apple have a very good document that covers the language.

    To directly answer the question if we assume <i>instance</i> is an instance of the class implementing the method, <i>tableInstance</i> is the UITableView instance we are dealing with and <i>section</i> is the section we are interested in then the message syntax is:
    Code:
    NSInteger rowsInSection = [instance tableView:tableInstance numberOfRowsInSection:section];
    
     
  5. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Thanks for your answer.
    I am reading that document what you suggested, but I couldn't find what I need so that I asked such simple question:(

    This way can only be used for message syntax, is it right?

    If use dot syntax, I think it should look like as below, do you think so?
    Code:
    - (NSInteger)numberOfRowsInSection:(UITableView *)tableView  sectionInTable:(NSInteger)section {
        // Return the number of time zone names.
        return [timeZoneNames count];
    }
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    I don't see what this code has to do with dot syntax. Dot syntax is only used for properties.
     
  7. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    Yeah, your are right, dot syntax is only for properties.

    But I am still not sure about label of second argument, that doesn't explain the meaning of that argument...
    numberOfRowsInSection:section
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    The meaning of any argument is 100% down to the exact method being called. In this case it is the section that the table view wants to the number of rows in. The argument is simply section (a NSInteger). The numberOfRowsInSection is simply a name that is human readable to indicate to the caller what this argument is for (to prevent questions like this).
     
  9. macrumors 6502a

    Joined:
    Nov 9, 2007
    #9
    You can use dot syntax for any getter method, it doesn't have to be a declared property. Whether you do or not is a matter of style but I tend to follow the rule of using dot syntax for queries but not commands. So I would use someArray.count but [someMutableArray removeAllObjects].
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Sure but I don't think you can use if for methods that take multiple arguments?
     
  11. macrumors 6502a

    Joined:
    Nov 9, 2007
    #11
    Nope, you can't. Perhaps its clearer to say I use dot-syntax for property-ish queries (which may or may not declared properties).
     

Share This Page