Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define and call a method with multiple parameters [duplicate]

Tags:

objective-c

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?

like image 661
SpokaneDude Avatar asked Oct 08 '12 16:10

SpokaneDude


People also ask

How do you pass multiple parameters in a method?

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.

How do I pass multiple parameters to a PowerShell function?

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.

Can a parameter have multiple values?

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.


2 Answers

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];
like image 77
DrummerB Avatar answered Sep 20 '22 18:09

DrummerB


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:

  • (NSArray ) fetchEventsBetweenStartDate:(NSDate) sDate andEndDate:(NSDate*) eDate;

Which would be called like [someObj fetchEventsBetweenStartDate: date1 andEndDate: date2];. Much clearer.

like image 30
bbum Avatar answered Sep 21 '22 18:09

bbum