【问题标题】:How to use NSNumbers to set an NSDate in a switch statement如何使用 NSNumbers 在 switch 语句中设置 NSDate
【发布时间】: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


    【解决方案1】:

    与其使用dateFromString 初始化器,不如直接从组件构建您的日期,并完全避免使用switch

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:daySection.intValue]; // <<== Extract int from daySection
    [comps setMonth:6];
    [comps setYear:2015];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];
    

    【讨论】:

      【解决方案2】:

      不要费心将index.section 转换为NSNumber;直接将其用作 switch 参数,如下所示:

      switch (index.section) {
      

      【讨论】:

        【解决方案3】:

        哦,我一开始没看到,你不能打开一个对象。必须是整数类型、字符类型或枚举类型。

        此外,您需要更改变量声明。裸初始化器不能在 switch 中使用。用花括号 { } 将 case 包裹起来,以局部限定变量声明。

        switch ([daySection integerValue]) {
            case 0: {
                NSDate *date = [dateFormatter dateFromString:@"2015-06-01"];
                break;
            }
            case 1: {
                NSDate *date = [dateFormatter dateFromString:@"2015-06-02"];
                break;
            }
            default:
                break;
        }
        

        【讨论】:

          【解决方案4】:

          在开关盒中:

          1. 直接使用index.section
          2. 使用[daySection integerValue]

          在 if-else 中,NSNumber 返回一个对象,因此将其与 1(一个整数)进行比较会返回一个警告。

          1. 使用[daySection integerValue]将其转换为整数
          2. 使用[daySection isEqualToNumber:@1][daySection isEqual:@1] 将NSNumber 与对象@1 进行比较

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-19
            相关资源
            最近更新 更多