以下语法对于枚举是否正确? [英] Is the following syntax is correct for an enum?

查看:130
本文介绍了以下语法对于枚举是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$
ValidationSessionTokenExpired = 2001,
ValidationSessionTokenInvalid = 2002,
ValidationEmailNotFound = 2003
验证SucccesMIN = ValidationLoginFailed,
ValidationSucccesMAX = ValidationEmailNotFound,
ValdationValidSuccessCode = 9999,
ValdationInvalidCode = 10000

};
typedef int ValidationStatusCodes;

请帮助我。

解决方案

在您的代码中, ValidationStatusCodes 表示 int ,而不是您的匿名枚举类型。所以他们并不是以任何方式连接起来。



然而,由于你的枚举包含 int 值,你可以说有某种关系。您可以传递枚举值的名称,它们将被视为 int ValidationStatusCodes 类型。



顺便说一句,苹果做了类似于你所做的事情,除了他们的集体名称 typedef NSInteger 或 NSUInteger 而不是 int uint 。有关示例,请参见此问题



所有这些说,一个更常见的做法是将 typedef 您的自定义类型名称直接转换为匿名 enum ,如下所示:

  typedef枚举{
ValidationLoginFailed = 2000,
ValidationSessionTokenExpired = 2001,
ValidationSessionTokenInvalid = 2002,
ValidationEmailNotFound = 2003
ValidationSuccessMIN = ValidationLoginFailed,
ValidationSuccessMAX = ValidationEmailNotFound,
ValdationValidSuccessCode = 9999,
ValdationInvalidCode = 10000
} ValidationStatusCodes;


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

}; 
typedef int ValidationStatusCodes;

please help me out.

解决方案

In your code, ValidationStatusCodes means int, not your anonymous enum type. So they aren't actually connected in any way.

However, since your enum contains int values, you could say that there's some sort of relation. You can pass the names of the enumerated values and they will be considered of the int or ValidationStatusCodes type.

By the way, Apple does something similar to what you do, except they typedef their collective names to NSInteger or NSUInteger instead of int or uint. See this question for an example.

With all that said, a more common practice is to typedef your custom type name directly to the anonymous enum, like this:

typedef enum {
    ValidationLoginFailed = 2000,
    ValidationSessionTokenExpired = 2001,
    ValidationSessionTokenInvalid = 2002,
    ValidationEmailNotFound = 2003
    ValidationSuccessMIN = ValidationLoginFailed,
    ValidationSuccessMAX = ValidationEmailNotFound,
    ValdationValidSuccessCode = 9999,
    ValdationInvalidCode = 10000
} ValidationStatusCodes;

这篇关于以下语法对于枚举是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆