iOS - 这是枚举的任务吗? [英] iOS - Is this a task for enums?
问题描述
如果我想实现以下代码,则枚举是否合适?我已经查询了一些关于枚举的问题,但我还是不太确定。
If I want to implement the following code, would enums be appropriate? I've looked up a few questions on enums, but I'm still a bit unsure.
if (dayOfWeek == Monday)
{
// Do something
}
else if (dayOfWeek == Tuesday || dayOfWeek == Wednesday)
{
// Do something else
}
如果这样似乎正确,我将如何初始化枚举?它会在标题或实现文件中进行吗?
If this seems right, how would I go about initialising the enum? Would it go in the header or implementation file?
推荐答案
如果我要实现以下代码,枚举是否合适?
If I want to implement the following code, would enums be appropriate?
没有关于替代方案的详细说明 - 是的。
Without getting too detailed about alternatives -- Yes.
我将如何初始化枚举?
how would I go about initialising the enum?
我通常在C中声明一个枚举像这样:
I typically declare an enum in C like so:
typedef enum MONDayOfWeek {
MONDayOfWeek_Undefined = 0,
MONDayOfWeek_Monday,
MONDayOfWeek_Tuesday,
MONDayOfWeek_Wednesday,
MONDayOfWeek_Thursday,
MONDayOfWeek_Friday,
MONDayOfWeek_Saturday,
MONDayOfWeek_Sunday
} MONDayOfWeek;
// in use:
MONDayOfWeek day = MONDayOfWeek_Monday;
MON
将是您的图书馆或组织的前缀。 DayOfWeek
将是库中的枚举名称,然后附加值。
MON
would be your library or organization's prefix. DayOfWeek
would be the enum name within the library, then the values are appended.
虽然这很有用,以避免冲突相当好。
Although it's wordy, you tend to avoid collisions quite well.
它会在标题或实现文件中吗?
Would it go in the header or implementation file?
在标题中,如果您希望它被多个文件使用,则在实现文件中。
In the header, if you want it to be used by multiple files, else in the implementation file.
这篇关于iOS - 这是枚举的任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!