tobbe
tobbe

Reputation: 1807

Declaration of many methods in header

Im trying to learn obj-c and here's something I dont understand. I know some c# so could someone try to explain this in in a c#y-way I maybe could understand it better :)

#import <Foundation/Foundation.h> @interface Car : NSObject
@interface Car : NSObject
  {
  int year;
  NSString *make;
  NSString *model;
  }

  **- (void) setMake:(NSString *) aMake andModel:(NSString *) aModel andYear: (int) aYear;**
  - (void) printCarInfo;
  - (int) year;
@end

I think i understand the declarations of the variables, but i dont get it for the metod(s?). Can someone explain what this do (bold code)?

Upvotes: 2

Views: 91

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

The concept which is missing from your learning is "selector". It's essentially like "decorating" a method signature with more semantics than you can get with parenthesized parameter-passing. We tend to use descriptive, sentence-like "selectors" for our method prototypes. As an example, consider this basic function:

void function setDimensions(int width, int height);

in Objective-C, we would write it like this:

- (void) setWidth:(int) newWidth height:(int) newHeight;

and the "selector" (which is a first-class concept in Objective-C) is setWith:height: (including the colons, but not anything else). That's the thing that uniquely identifies that method on that class.

Calling that method happens at runtime when you send a message to that object with arguments placed after the colons in the selector. So a message has three essential components: a recipient, a selector, and the well-placed arguments:

[anObject setWidth: 1024 height: 768];

Here, anObject is the recipient, setWidth:height: is the selector (helps the runtime find the method), and the arguments are: newWidth = 1024; newHeight = 768;.

Then in the implementation of the method, simply use newWidth and newHeight like you would expect:

- (void) setWidth:(int) newWidth height:(int) newHeight; {
    self.width = newWidth; // 1024
    self.height = newHeight; // 768
}

It may seem awkward and superfluous at first, but after you get used to it, you will prefer it and find yourself writing longer, more semantic function names in other languages.

The concepts associated with selectors are all better understood when learned along with some of the basics of the Objective-C runtime, specifically the concept of "sending messages" (instead of "calling methods") and the fact that at runtime, not compile-time the actual method is looked-up based on the selector you use in the message. The fact that this happens at runtime gives remarkable flexibility for highly dynamic architectures.

Upvotes: 2

DrummerB
DrummerB

Reputation: 40211

In Objective-C methods have named arguments. The method declarations syntax is like this:

- (returnValue)nameOfArgument1:(typeOfArgument1)nameOfVariable1 
               nameOfArgument2:(typeOfArgument2)nameOfVariable2 
               nameOfArgument3:(typeOfArgument3)nameOfVariable3;

The line breaks are of course optional, but you need to separate the arguments with at least one space. The - sign on the first line means, that this is an instance method (as opposed to a class method, which should begin with a + sign).

The implementation in your .m file looks exactly the same, except you replace the semicolon with the implementation:

- (returnValue)nameOfArgument1:(typeOfArgument1)nameOfVariable1 
               nameOfArgument2:(typeOfArgument2)nameOfVariable2 
               nameOfArgument3:(typeOfArgument3)nameOfVariable3 {
    // Implementation
    // Return something of type "returnValue".
}

And you would call this method from an other place like this:

[myObject nameOfArgument1:variableToPass1 
          nameOfArgument2:variableToPass2
          nameOfArgument3:variableToPass3];

I hope this makes things a bit more clear.

Upvotes: 0

Related Questions