在 Objective-C 枚举中查找整数值

发布于 2024-10-05 15:58:10 字数 582 浏览 1 评论 0原文

Objective-C 中是否可以查看 int 值是否在特定的 enum 中?例如,在此枚举中:

enum  {
  ValidationLoginFailed         = 2000,
  ValidationSessionTokenExpired = 2001,
  ValidationSessionTokenInvalid = 2002,
  ValidationEmailNotFound       = 2003  
}; 
typedef int ValidationStatusCodes;

是否可以查看 ValidationStatusCodes enum 中是否存在任意整数值?

[ValidationStatusCodes contains:intResponseCode]

或者至少

[self intIsInRangeofEnum:ValidationStatusCodes forValue:intResponseCode]

Is it possible in Objective-C to see if a int value is in a particular enum? For instance, in this enum:

enum  {
  ValidationLoginFailed         = 2000,
  ValidationSessionTokenExpired = 2001,
  ValidationSessionTokenInvalid = 2002,
  ValidationEmailNotFound       = 2003  
}; 
typedef int ValidationStatusCodes;

is it possible to see if an arbitrary integer value is in the ValidationStatusCodes enum?

[ValidationStatusCodes contains:intResponseCode]

or at least

[self intIsInRangeofEnum:ValidationStatusCodes forValue:intResponseCode]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

┾廆蒐ゝ 2024-10-12 15:58:10

没有比直接做更简单的方法了

(ValidationLoginFailed == intResponseCode ||
ValidationSessionTokenExpired == intResponseCode ||
ValidationSessionTokenInvalid == intResponseCode ||
ValidationEmailNotFound == intResponseCode)

。一般来说,C 对于做动态事情或反映类型没有太大帮助,而枚举是 C 的一项功能。

There's no simpler way than just doing

(ValidationLoginFailed == intResponseCode ||
ValidationSessionTokenExpired == intResponseCode ||
ValidationSessionTokenInvalid == intResponseCode ||
ValidationEmailNotFound == intResponseCode)

In general, C is not very helpful for doing dynamic things or reflecting on types, and enums are a C feature.

好多鱼好多余 2024-10-12 15:58:10

不使用枚举enum 不是 Objective-C 对象,因此您无法像现在这样向它发送消息。

也许使用 NSDictionary ?

Not with an enum. An enum is not an objective-C object, so you can't send it messages as you're doing.

Maybe use an NSDictionary?

随波逐流 2024-10-12 15:58:10

这个问题有点过时了,但我在软件设计中看到的标准是使用位掩码,其中每个值都是带有位移位的离散状态。在某些情况下,您的枚举值可以是其他值的组合。

enum  {
  ValidationLoginFailed         = 0, //0
  ValidationSessionTokenExpired = 1 << 0, //1
  ValidationSessionTokenInvalid = 1 << 1, //2
  ValidationEmailNotFound       = 1 << 2  //4
}; 
typedef int ValidationStatusCodes;

对于您的用例,您将 & 包含集合中所有项目的结果:

int allStates = (ValidationLoginFailed | ValidationSessionTokenExpired | 
   ValidationSessionTokenInvalid | ValidationEmailNotFound); //7

if(val & allStates){
   //some logic here
}

This question is a little dated, but the standard I have seen in software design is to use a bitmask where each of these values is a discrete state with a bit-shift. In some cases, your enum values can be combinations of other values.

enum  {
  ValidationLoginFailed         = 0, //0
  ValidationSessionTokenExpired = 1 << 0, //1
  ValidationSessionTokenInvalid = 1 << 1, //2
  ValidationEmailNotFound       = 1 << 2  //4
}; 
typedef int ValidationStatusCodes;

For your use-case you would & your result with all of the items in the set:

int allStates = (ValidationLoginFailed | ValidationSessionTokenExpired | 
   ValidationSessionTokenInvalid | ValidationEmailNotFound); //7

if(val & allStates){
   //some logic here
}
半枫 2024-10-12 15:58:10

好吧,只要这个问题再提出来。有一个很好的开源项目 JREnum

它允许执行以下操作:

JREnumDeclare( ValidationStatusCodes,
  ValidationLoginFailed         = 2000,
  ValidationSessionTokenExpired = 2001,
  ValidationSessionTokenInvalid = 2002,
  ValidationEmailNotFound       = 2003  
); 

然后 ValidationStatusCodesByValue() 返回 NSDictionary,其键对应于 NSNumber。所以:

if ([ValidationStatusCodesByValue() objectForKey:@(intResponseCode)])
    ...

Well, as long as this question is up again. There's a nice open source project JREnum

Which allows to do the following thing:

JREnumDeclare( ValidationStatusCodes,
  ValidationLoginFailed         = 2000,
  ValidationSessionTokenExpired = 2001,
  ValidationSessionTokenInvalid = 2002,
  ValidationEmailNotFound       = 2003  
); 

And then ValidationStatusCodesByValue() returns NSDictionary which keys are corresponding NSNumbers. So:

if ([ValidationStatusCodesByValue() objectForKey:@(intResponseCode)])
    ...
时光是把杀猪刀 2024-10-12 15:58:10

[更新]

在一些 C++ 帖子中发现了这个方法,虽然不是完全动态的,但可以用最小的麻烦完成这个技巧:

将范围范围添加到枚举条目:

enum  {
  ValidationLoginFailed=2000,
  ValidationSessionTokenExpired=2001,
  ValidationSessionTokenInvalid=2002,
  ValidationEmailNotFound=2003
  ValidationSucccesMIN=ValidationLoginFailed,
  ValidationSucccesMAX=ValidationEmailNotFound,
  ValdationValidSuccessCode=9999,
  ValdationInvalidCode=10000

}; 
typedef int ValidationStatusCodes;

然后沿着这些线进行操作:

-(ValidationStatusCodes)isReponseCodeValid{

for (int i=ValidationSucccesMIN; i<=ValidationSucccesMAX; i++) {
    if(returnCode==i){
        return ValdationValidSuccessCode;
    }
}

return ValdationInvalidCode;
}

显然最终的代码远不止这 4 个代码app 中也会出现相应的错误块。

[UPDATE]

Found this method in some C++ posts which, although not entirely dynamic, does the trick with minimal fuss:

Add range extents to enum entries:

enum  {
  ValidationLoginFailed=2000,
  ValidationSessionTokenExpired=2001,
  ValidationSessionTokenInvalid=2002,
  ValidationEmailNotFound=2003
  ValidationSucccesMIN=ValidationLoginFailed,
  ValidationSucccesMAX=ValidationEmailNotFound,
  ValdationValidSuccessCode=9999,
  ValdationInvalidCode=10000

}; 
typedef int ValidationStatusCodes;

then something along these lines:

-(ValidationStatusCodes)isReponseCodeValid{

for (int i=ValidationSucccesMIN; i<=ValidationSucccesMAX; i++) {
    if(returnCode==i){
        return ValdationValidSuccessCode;
    }
}

return ValdationInvalidCode;
}

obviously there are far more than just these 4 codes in the final app and there will a block of corresponding error ones too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文