5

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?

Community
  • 1
  • 1
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 1
    The name of the method at definition and call are _completely_ different. Where does that "withEndDate:" part come from? What did you expect to happen? – jscs Oct 08 '12 at 17:01

2 Answers2

15

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];
raoulsson
  • 14,978
  • 11
  • 44
  • 68
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Thank you so much! That makes complete sense, once I have an example to look at. I find the Apple Docs somewhat confusing, so this is what I needed. However, I am getting a warning on the method call: Instance method '-fetchEventsBetweenDate:andDate:' not found (return type defaults to 'id') What is it talking about? – SpokaneDude Oct 08 '12 at 17:16
  • What object are you calling this message on? Paste what you're doing, otherwise I can't help – DrummerB Oct 08 '12 at 17:19
  • I updated the code to reflect what I have now... still getting the error in previous post... – SpokaneDude Oct 08 '12 at 17:29
  • Do you really comment like that? In Objective-C (and C) you comment with `//`, not with `-`. – DrummerB Oct 08 '12 at 17:42
  • I found it! (misplaced colon)... thank you so much for the example... and no, I don't comment like that... I've seen others do it that way when posting here, and I just assumed it was the norm. – SpokaneDude Oct 08 '12 at 17:42
  • Hmm, it's not `- (return type)myMethodParam1: param1:(param1 type)p1 param2:(param2 type)p2;` that's really confusing ... sigh – daisy Jan 12 '15 at 09:16
  • BTW the "Message Syntax" url is gone, 404-ed – daisy Jan 12 '15 at 09:20
5

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.

bbum
  • 162,346
  • 23
  • 271
  • 359