枚举值到 NSString (iOS)
我有一个包含多个值的枚举:
enum {value1, value2, value3} myValue;
在我的应用程序中的某个点,我希望检查枚举的哪个值现在处于活动状态。我正在使用 NSLog,但我不清楚如何将枚举的当前值(value1/valu2/valu3/等...)显示为 NSLog 的 NSString。
有人吗?
I have an enum holding several values:
enum {value1, value2, value3} myValue;
In a certain point in my app, I wish to check which value of the enum is now active. I'm using NSLog but I'm not clear on how to display the current value of the enum (value1/valu2/valu3/etc...) as a NSString for the NSLog.
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我不喜欢将枚举放在堆上,而不提供用于翻译的堆函数。这就是我的想法:
这使枚举和字符串声明紧密结合在一起,以便在需要时轻松更新。
现在,在代码中的任何位置,您都可以像这样使用枚举/宏:
为了保证元素索引,您始终可以显式声明起始(或所有)枚举值。
I didn't like putting the enum on the heap, without providing a heap function for translation. Here's what I came up with:
This keeps the enum and string declarations close together for easy updating when needed.
Now, anywhere in the code, you can use the enum/macro like this:
To guarantee the element indexes, you can always explicitly declare the start(or all) enum values.
这里回答了这个问题:一些关于实现的建议
行是
Objective-C
正在使用常规的旧C
enum
,它只是一组美化的整数。给定一个像这样的
enum
:您的方法将如下所示:
This is answered here: a few suggestions on implementation
The bottom line is
Objective-C
is using a regular, oldC
enum
, which is just a glorified set of integers.Given an
enum
like this:Your method would look like this:
我介绍的是我使用的方式,看起来比之前的答案更好。(我认为)
我想用UIImageOrientation来说明,以便于理解。
创建一个如下方法:
您所要做的就是:
为您的函数命名。
复制枚举的内容并将其粘贴到NSArray *arr = @[和]之间; return (NSString *)[arr objectAtIndex:input];
放置一些@、"和逗号
利润!!!!!
I will introduce is the way I use, and it looks better than previous answer.(I thinks)
I would like to illustrate with UIImageOrientation for easy understanding.
create a method like:
All you have to do is :
name your function.
copy contents of enum and paste that between NSArray *arr = @[ and ]; return (NSString *)[arr objectAtIndex:input];
put some @ , " , and comma
PROFIT!!!!
这将由编译器验证,因此您不会意外地混淆索引。
更新:一种更快速的方法是使用
CustomStringConvertible
一致性来扩展枚举。此外,通过这种方式,编译器将确保实现底层枚举的每个新添加(而使用数组则不然),因为switch
语句必须是详尽的。This will be validated by compiler, so you won't mix up indices accidentally.
UPDATE: A more Swifty way is to extend the enum with
CustomStringConvertible
conformance. Also, this way the compiler will safeguard to implement every new addition to the underlying enum (whereas using arrays does not), asswitch
statements must be exhaustive.我找到了这个网站(下面的例子就来自于此),它为这个问题提供了一个优雅的解决方案。最初的帖子来自这个 StackOverflow 答案。
I found this website (from which the example below is taken) which provides an elegant solution to this problem. The original posting though comes from this StackOverflow answer.
如果我可以提供另一种解决方案,该解决方案具有类型检查的额外好处,如果您在转换中缺少枚举值,则会发出警告,提高可读性和简洁性。
对于您给定的示例:
typedef enum { value1, value2, value3 } myValue;
您可以这样做:作为旁注。如下声明枚举是一种更好的方法:
通过这种方式,您可以获得类型安全(在本例中为
NSInteger
),您可以设置预期的枚举偏移量 (= 0
) 。If I can offer another solution that has the added benefit of type checking, warnings if you are missing an enum value in your conversion, readability, and brevity.
For your given example:
typedef enum { value1, value2, value3 } myValue;
you can do this:As a side note. It is a better approach to declare your enums as so:
With this you get type-safety (
NSInteger
in this case), you set the expected enum offset (= 0
).在某些情况下,当您需要转换枚举时 -> NSString 和 NSString -> enum 使用 typedef 和 #define (或 const NSStrings)代替 enum 可能会更简单:
然后像任何其他 NSString 一样使用“命名”字符串进行操作:
In some cases when you need to convert enum -> NSString and NSString -> enum it might be simpler to use a typedef and #define (or const NSStrings) instead of enum:
and then just operate with "named" strings as with any other NSString:
下面的解决方案使用预处理器的字符串化运算符,从而提供更优雅的解决方案。它允许您在一个地方定义枚举术语,以提高对拼写错误的抵御能力。
首先,按以下方式定义枚举。
现在,按以下方式使用它:
输出:
@pixel 的答案为我指明了正确的方向。
The solution below uses the preprocessor's stringize operator, allowing for a more elegant solution. It lets you define the enum terms in just one place for greater resilience against typos.
First, define your enum in the following way.
Now, use it in the following way:
which outputs:
@pixel's answer pointed me in the right direction.
您可以使用 X 宏 - 它们非常适合此目的。
好处
1. 实际枚举值和字符串值之间的关系在一处。
2. 您可以稍后在代码中使用常规 switch 语句。
损害
1. 初始设置代码有点迟钝,并且使用了有趣的宏。
代码
现在您可以执行以下操作:
此模式自 1960 年代以来就已存在(不是开玩笑!):http://en.wikipedia.org/wiki/X_Macro
You could use X macros - they are perfect for this.
Benefits
1. the relationship between the actual enum value and the string value is in one place.
2. you can use regular switch statements later in your code.
Detriment
1. The initial setup code is a bit obtuse, and uses fun macros.
The code
Now you can do:
This pattern has been around since the 1960's (no kidding!): http://en.wikipedia.org/wiki/X_Macro
这是一个即插即用的解决方案,您可以通过简单复制和粘贴现有定义来扩展它。
我希望你们都觉得它很有用,就像我发现许多其他 StackOverflow 解决方案很有用一样。
以下是示例声明:
这是一个示例调用:
Enjoy!
;-)
Here is a plug-and-play solution that you can extend with a simple copy and paste of your EXISTING definitions.
I hope you all find it useful, as I have found useful so many other StackOverflow solutions.
Here are the example declarations:
Here is an example call:
Enjoy!
;-)
下面是一个对 Objective-C 友好的 Enum Struct 示例,如果您需要在用 Objective-C 编写的旧项目中使用 Swift 代码的话。
示例:
contentType.文件名。 toString()
内容类型.文件名。 原始值
Below is a example of Enum Struct that is Objective-C friendly in the event you need to use Swift Code in Legacy projects written in Objective-C.
Example:
contentType.filename. toString()
contentType.filename. rawValue
这是一个老问题,但是如果您有一个不连续的枚举,请使用字典文字而不是数组:
This is an old question, but if you have a non contiguous enum use a dictionary literal instead of an array:
假设要求是枚举语言列表。
将其添加到 .h 文件中
现在,在 .m 文件中只需创建一个数组,例如,
就是这样。现在您可以使用 easy 枚举并使用数组获取枚举字符串。例如,
因此,此模式取决于 NS_ENUM 和同伴 ToString 数组的可接受的命名约定。尝试完全遵循这个惯例,它就会变得自然。
Suppose requirement is to enumerate list of languages.
Add this to .h file
Now, in .m file simply create an array like,
Thats it. Now you can use enum with easy and get string for enums using array. For example,
Thus, this pattern depends on accepted naming convention of NS_ENUM and companion ToString array. Try to follow this convention through out and it will become natural.
这类似于按像素发布的“X”宏。
的链接
感谢您提供 http://en.wikipedia.org/wiki/X_Macro中生成的代码 宏可能很棘手且难以调试。
相反,生成一个由“正常”代码使用的表。
我发现很多人反对让宏生成代码,
这可能是采用“X Macros”技术的原因之一
在 wiki 中并未被广泛采用。
通过生成表格,您仍然只需要编辑一处即可扩展列表,
由于您无法在调试器中“单步执行”表,因此这会删除
许多人反对将多行代码隐藏在宏中。
This is similar to the "X" macro post by pixel.
thanks for the link to http://en.wikipedia.org/wiki/X_Macro
Code generated in macros can be tricky and hard to debug.
Instead, generate a table that is used by "normal" code.
I find that many people object to having macros generate code,
and may be one reason the technique of "X Macros" as presented
in the wiki is not widely adopted.
By generating a table, you still need only edit one place to extend the list,
and since you can't "step through" a table in the debugger, this removes the
objection from many people about multi-lines of code buried in macros.
宏:
枚举:
数组:
使用:
==== 编辑 ====
将以下代码复制到您的项目并运行。
a macro:
an enum:
an array:
using:
==== EDIT ====
Copy the following code to your project and run.
这是工作代码
https://github.com/ndpiparava/ObjcEnumString
Here is working code
https://github.com/ndpiparava/ObjcEnumString