Possible Duplicate:
How to write a method/message with multiple parameters?
I am really confused here... I have looked at SO and Google where I found an example of calling a method with two parameters. So I modified it for my use, and unfortunately I can't get it to work. Here is my updated code:
- definition of method
- (NSArray *) fetchEventsBetweenDates: (NSDate *) sDate: andDate: (NSDate *) eDate;
- definitions and creation of sD and eD
// convert start dates to NSDate
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* sD = [df dateFromString:@"10/03/2012"];
NSLog(@"startDate: %@", sD);
// convert end dates to NSDate
NSDate* eD = [df dateFromString:@"10/05/2012"];
NSLog(@"endDate: %@", eD);
- call to method
[self.eventsList addObjectsFromArray:[self fetchEventsBetweenDates: sD andDate: eD]];
- method
- (NSArray *) fetchEventsBetweenDates: (NSDate *) sDate: andDate: (NSDate *) eDate {
I have tried every permutation I think was reasonable, and it still will not build. I am getting a "expected ':' on the call to the method.
What am I doing wrong?
Parameters and Arguments Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.
You can add a parameter to a paginated report that allows the user to select more than one value for the parameter. You can pass multiple parameter values to the report within the report URL.
Please read The Objective-C Programming Language from Apple. Specifically the Message Syntax part.
The message declaration syntax is like this:
- (return type)myMethodParam1:(param1 type)p1 param2:(param2 type)p2;
Example:
- (NSArray *)fetchEventsBetweenDate:(NSDate *)startDate andDate:(NSDate *)endDate;
The implementation is the same, you just replace the semicolon ;
with the implementation in curly brackets { implementation }
.
When calling a method, you replace the parameter declarations with your variables:
// Assuming aDate and otherDate exist.
[self fetchEventsBetweenDate:aDate andDate:otherDate];
Start with a basic Objective-C book. Pretty clear you don't understand the syntax or language yet (no big deal -- we all started there).
If you really had a method defined as:
- (NSArray *) fetchEventsForADay: (NSDate *) sDate: (NSDate *) eDate;
You would call it using [someObj fetEventsForADay: date1 : date2];
That would suck; the whole point of Objective-C's interleave-arguments-with-method-name pattern is to make APIs self documenting. Thus, you'd probably want:
Which would be called like [someObj fetchEventsBetweenStartDate: date1 andEndDate: date2];
. Much clearer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With