【发布时间】:2015-06-23 18:15:05
【问题描述】:
我想要做的是将UITableViews 部分索引映射到相应的NSDate,我最初想这样做:
-(BOOL)whatSectionsAreVisible {
NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
for (NSIndexPath *index in visibleRowIndexes) {
NSNumber *daySection = @(index.section);
// Here is where I will map every index.section to an NSDate
static NSDateFormatter *dateFormatter = nil;
if(!dateFormatter){
dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy-MM-dd"; // Read the documentation for dateFormat
}
if (daySection == 0){
NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
}
else if (daySection == 1){
NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
}
//... and so on
}
但是,使用 if 语句执行此操作 30 天会变得非常冗长,我认为在这种情况下使用 switch 语句会更有意义。我无法弄清楚如何设置 switch 语句的语法,我尝试这样做:
switch (daySection) {
case 0:
NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
break;
case 1:
NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
break;
default:
break;
}
但是第一行给了我错误Statement requires expression of integer type ('NSNumber *__strong' invalid)。如何正确设置此语句?
旁注:else if (daySection == 1) 行警告我正在比较指针和整数(NSNumber 和 int)。我该如何正确地进行比较?
【问题讨论】:
标签: ios uitableview switch-statement