【问题标题】:iOS: Why does NSRange always throw error "use of undeclared identifier"?iOS:为什么 NSRange 总是抛出错误“使用未声明的标识符”?
【发布时间】:2014-02-16 12:03:41
【问题描述】:

这几行代码就是问题所在:

case CostPeriodMonthly:
            NSRange days = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];

            [endDateComponents setDay:days.length];
            [endDateComponents setMonth:[todayComponents month]];
            [endDateComponents setYear:[todayComponents year]];
            break;

使用天数的第一行突出显示“使用未声明的标识符天数”,但为什么呢?我在这里想念什么?

【问题讨论】:

标签: ios nsrange


【解决方案1】:

如果您想在 case 语句中声明新变量,则必须将语句括在 {} 块中:

case CostPeriodMonthly: {
            NSRange days = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];

            [endDateComponents setDay:days.length];
            [endDateComponents setMonth:[todayComponents month]];
            [endDateComponents setYear:[todayComponents year]];
            break;
}

【讨论】:

  • 其实在case标签后面加一个分号就足够了。如果您使用 ARC 编译,则可能需要使用 { ... } 引入范围(请参阅“可能的重复项”)。
猜你喜欢
  • 1970-01-01
  • 2021-03-18
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多