 Table of Contents
Table of Contents  Previous Section
Previous Section
 
Methods
To define a new method, simply put its implementation in the script file. You don't need to declare it ahead of time. For example, this is the definition of a method from the Main component in the Visitors example:
	- recordMe {
		if ([aName length]) {
			[[self application] setLastVisitor:aName];	
			[self setAName:@""]; // clear the text field
		}
	}
Methods can take arguments. To define a method that takes arguments, you place the argument name after a colon (:). For example, the following method takes two arguments. It adds the two arguments together and returns the result:
	- addFirstValue:firstValue toSecondValue:secondValue {
		id result;
		result = firstValue + secondValue;
		return result;
	}
The strings that appear to the left of the colons are part of the method name. The method above is named addFirstValue:toSecondValue:. It takes two arguments, which it calls firstValue and secondValue. If you want, you can add type information for the return values and parameter values. For example, the following method, subtractFirstValue:fromSecondValue:, subtracts one number from another and returns the result:
	- (NSNumber *)subtractFirstValue:(NSNumber *)firstValue 
	fromSecondValue:(NSNumber *)secondValue {
		NSNumber *result;
		result = secondValue - firstValue;
		return result;
	}
In these examples, note the following:
- Type information is optional. When there is no type, id is assumed.
- Explicitly specifying the id type is not allowed:
	// NO!! This won't work.
	- (id)aMethod:(id)anArg { ... }
- A return type of void is not allowed:
	// This won't work either.
	- (void) aMethod:(NSString *)anArg { ... }
Both example methods return a value, stored in result. If a method doesn't return a meaningful value, you don't have to include a return statement (and, as stated above, even if a method returns no value you shouldn't declare it as returning void).
 
 Table of Contents
Table of Contents  Next Section
Next Section