可能重复:
如何编写包含多个参数的方法/消息?
我在这里真的很困惑......我看过SO和Google,在那里我找到了一个用两个参数调用方法的例子.所以我修改了它供我使用,不幸的是我无法让它工作.这是我更新的代码:
- 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 {
Run Code Online (Sandbox Code Playgroud)
我已经尝试过每一种我认为合理的排列,它仍然无法构建.我得到一个"预期的':'对方法的调用.
我究竟做错了什么?
Dru*_*erB 12
请阅读Apple 的Objective-C编程语言.特别是消息语法部分.
消息声明语法如下:
- (return type)myMethodParam1:(param1 type)p1 param2:(param2 type)p2;
Run Code Online (Sandbox Code Playgroud)
例:
- (NSArray *)fetchEventsBetweenDate:(NSDate *)startDate andDate:(NSDate *)endDate;
Run Code Online (Sandbox Code Playgroud)
实现是一样的,你只需用;大括号中的实现替换分号{ implementation }.
调用方法时,将参数声明替换为变量:
// Assuming aDate and otherDate exist.
[self fetchEventsBetweenDate:aDate andDate:otherDate];
Run Code Online (Sandbox Code Playgroud)
从一本基本的 Objective-C 书籍开始。很明显你还不了解语法或语言(没什么大不了的——我们都是从那里开始的)。
如果你真的有一个方法定义为:
- (NSArray *) fetchEventsForADay: (NSDate *) sDate: (NSDate *) eDate;
Run Code Online (Sandbox Code Playgroud)
你会用[someObj fetEventsForADay: date1 : date2]; That 来调用它。Objective-C 的 interleave-arguments-with-method-name 模式的重点是使 API 自我记录。因此,您可能想要:
这将被称为像[someObj fetchEventsBetweenStartDate: date1 andEndDate: date2];. 清晰多了。
| 归档时间: |
|
| 查看次数: |
18875 次 |
| 最近记录: |