View Full Version : Can you help me understand this line of code?
Glama
Jul 23, 2009, 08:32 AM
Hi,
I came across the code below in a book on iphone development:
- (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
Troglodyte
Jul 23, 2009, 08:44 AM
- (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.
Glama
Jul 23, 2009, 01:02 PM
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?
FarSeide
Jul 23, 2009, 01:20 PM
- (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 {
PhoneyDeveloper
Jul 23, 2009, 01:35 PM
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.
lloyddean
Jul 23, 2009, 03:02 PM
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?
Actually the name of the function is 'tableView:numberOfRowsInSection:' and takes 2 parameters.
Glama
Jul 23, 2009, 10:03 PM
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.
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.
Glama
Jul 23, 2009, 10:08 PM
Thanks FarSeide and lloyddean. Obj-c is tough to get used after years of
integer mymethod(param1, param2)
PhoneyDeveloper
Jul 23, 2009, 11:38 PM
What books do you have?
Have you read the sticky: iPhone Developer FAQ?
This is almost the same as Objective-C:
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
lloyddean
Jul 24, 2009, 03:36 PM
Thanks FarSeide and lloyddean. Obj-c is tough to get used after years of
integer mymethod(param1, param2)
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.
Glama
Jul 25, 2009, 10:56 AM
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.
Thanks lloyddean, it looks promising.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.