gpt4 book ai didi

iphone - 为什么我不能在 switch 语句中使用 NSInteger?

转载 作者:太空狗 更新时间:2023-10-30 03:31:18 25 4
gpt4 key购买 nike

为什么这行不通:

NSInteger sectionLocation = 0;
NSInteger sectionTitles = 1;
NSInteger sectionNotifications = 2;

switch (section) {
case sectionLocation:
//
break;
case sectionTitles:
//
break;
case sectionNotifications:
//
break;
default:
//
}

我得到这个编译错误:

error: case label does not reduce to an integer constant

难道不能像这样使用 NSInteger 吗?如果是这样,是否有另一种方法可以在 switch 语句中使用变量作为案例? sectionLocation 等具有可变值。

最佳答案

问题不在于标量类型,而是当它们是这样的变量时 case 标签可能会改变值。

出于所有意图和目的,编译器将 switch 语句编译为一组 goto。标签不能可变。

使用枚举类型或#defines。

关于iphone - 为什么我不能在 switch 语句中使用 NSInteger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com