Can you help me understand this line of code?

Discussion in 'iOS Programming' started by Glama, Jul 23, 2009.

  1. Glama macrumors member

    Joined:
    Feb 5, 2009
    #1
    Hi,
    I came across the code below in a book on iphone development:
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [some code here]
    
    return rows;
    }
    
    I dont understand the first line. I understand from the accompanying text in the book that numberOfRowsInSection is the name of the method, so I guess the rest of the line means that it accepts a NSInteger which it refers to as "section". But the first half of the line baffles me. Since the method returns a variable called "rows", I assume that is an integer so Im thinking the "NSInteger" at the beginning of the line specifies the return type. So what is the "tableview:(UITableView *)tableView" part for? What does it specify?

    Phil
     
  2. Troglodyte macrumors member

    Joined:
    Jul 2, 2009
    #2
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    The method returns an NSInteger and takes two parameters - one is a pointer to a UITableView object, the other is an NSInteger indicating the section in that UITableView that you are to return the number of rows of.
     
  3. Glama thread starter macrumors member

    Joined:
    Feb 5, 2009
    #3
    Thanks Troglodyte.
    So one parameter comes before the method name, and one comes after. As a newbie to objective-c, that seems so odd. Any way to make sense of that? What if it took 3 input parameters? Where would the next one go?
     
  4. FarSeide macrumors 6502a

    FarSeide

    Joined:
    Feb 17, 2008
    Location:
    Earth Lane
    #4
    - (NSInteger) = Return Type
    tableView: = Method Name
    (UITableView *) = Data Type it accepts (In this case it accepts a pointer to a UITableView)
    tableView = Parameter Name
    numberOfRowsInSection: = Method Name with Second Parameter
    (NSInteger) = Data Type it accepts (In this case it accets NSInteger which is a wrapper class for int)
    section = Parameter Name

    *if there was a third parameter, it would look like this...
    {

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section someOtherParameter: (NSInteger) someOtherInteger {
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Glama, you need to get a book on learning Objective-C and another one on learning iPhone development. You will learn more quickly that way.
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Actually the name of the function is 'tableView:numberOfRowsInSection:' and takes 2 parameters.
     
  7. Glama thread starter macrumors member

    Joined:
    Feb 5, 2009
    #7
    I have one, but it focuses on bigger concepts like inheritance, etc. I havent found a good source of info on the actually syntax and "grammar" of objective-C.
     
  8. Glama thread starter macrumors member

    Joined:
    Feb 5, 2009
    #8
    Thanks FarSeide and lloyddean. Obj-c is tough to get used after years of

    integer mymethod(param1, param2)
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    What books do you have?

    Have you read the sticky: iPhone Developer FAQ?

    This is almost the same as Objective-C:

    Code:
    integer mymethod(type param1, type param2)
    
    and 
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
    integer == NSInteger

    mymethod == tableView:numberOfRowsInSection:

    type param1 = UITableView*

    type param2 = NSInteger

    param1 = tableView

    param2 = section
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #10
    While I haven't read it this document http://ktd.club.fr/programmation/fichiers/cpp-objc-en.pdf
    looks like it might prove helpful to C/C++ programmers coming to Objective-C.
     
  11. Glama thread starter macrumors member

    Joined:
    Feb 5, 2009
    #11

Share This Page