Simple Question on Syntax

Discussion in 'Mac Programming' started by -Garry-, Apr 13, 2009.

  1. macrumors 6502a

    -Garry-

    Joined:
    Oct 23, 2005
    Location:
    Manchester, UK
    #1
    Hi, I know this is really basic but I've been working my way through several books I bought - starting off with basic Objective-C and then onto Cocoa and Interface Builder and I have a question on some syntax I don't think I've seen before. I was hoping somebody could explain it.

    In an implementation file I understand you have to define the implementation of your own classes as follows:

    Code:
    -(void) print 
    { 
    NSLog (@”%i/%i”, numerator, denominator); 
    } 
     
    @end 
    The top line is the line I'm interested in. That's the syntax I've been using so far and I understand it.

    I'm now seeing the following:

    Code:
    - (int)numberOfRowsInTableView:(NSTableView *)tv
    {
        return [voiceList count];
    }
    Again, the top line is the one I'm interested in. I understand it's saying that the method being defined (numberOfRowsInTableView) returns an integer. It's the second bit (NSTableView *)tv that I don't understand.

    I believe it has something to do with passing arguments into the method but I don't understand the syntax. Can anyone explain?
     
  2. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #2
    This indicates that this method take one parameter and returns an int. The parameter is of type NSTableView* and locally we are calling that parameter 'tv'.

    So in the method implementation code we can now refer to the parameter as 'tv'.

    Some table view could call this method like so:

    Code:
    int a = [someObject numberOfRowsInTableView: self];
    
    // or someone may do something like this:
    
    int a = [someObject numberOfRowsInTableView: someOtherTableView];
    
    If you are familiar with any other programming language, this is exactly the same concept as function or method parameters in other languages. Just the syntax is a bit different. So in C you might see something similar as:

    Code:
    int numberOfRowsInTableView(NSTableView* tv) {...}
    
    // or in python
    
    def numberOfRowsInTableView (tv):
    
    
    Hope that helps?
     
  3. thread starter macrumors 6502a

    -Garry-

    Joined:
    Oct 23, 2005
    Location:
    Manchester, UK
    #3
    Hi Eddie,

    That does help, thank you.

    What about the asterisk? Is that declaring that we're not actually passing an object in as an argument - we're passing a pointer in instead?

    If so, is there any particular reason it's (NSTableView *)tv rather than NSTableView(*tv) or is it just 'one of those things?'
     
  4. macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #4
    All references to objects in Objective-C are pointers.

    Notice you always declare:

    NSObject *anObject;

    never

    NSObject anObject;

    as for the "funky" syntax with the parentheses: just the Objective-C way of doing things.

    Think of it as a type cast: (NSTableView *)tv : tv is being cast as a pointer to NSTableView. This isn't technically correct but helped me when I was learning the ropes.
     

Share This Page