在哪里创建以及如何在 iOS 中使用 Enum?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22195455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:38:56  来源:igfitidea点击:

Where to create and how to use Enum in iOS?

iosobjective-cenums

提问by Jeeva Jsb

I have started learning iOS development.

我已经开始学习iOS开发。

I want to use enumin my sample project.

我想enum在我的示例项目中使用。

I've declared enumin sample.hlike following. I hope I've declared this correctly.

我已经声明enumsample.h像下面。我希望我已经正确地声明了这一点。

typedef enum{s=1,m,t,w,th,f,sa} days;

I want to use this in viewController.m. In viewController.h,I've imported sample.h.

我想在viewController.m. 在viewController.h,我已经导入了sample.h.

I want to use enum with the name like "days.sa". But more code i searched in google, they've said like create a instance variable in "sample.h"like

我想使用 enum 之类的名称"days.sa"。但是我在谷歌搜索的更多代码,他们说像创建一个实例变量"sample.h"一样

@interface Sample:NSObject
{
    days d;
}

If I want to use this means, I need to create and use instance. But I don't want like that.

如果我想使用这种方式,我需要创建和使用实例。但我不想那样。

I need to use like

我需要使用像

days.d or days.sa or days.th

How to do that ?, This must be used for the whole Project and

怎么做?,这必须用于整个项目和

How to create enum as class variable instead of instance variable ?

如何将枚举创建为类变量而不是实例变量?

回答by stefandouganhyde

In the enum you've created, s, metc. are now available globally (i.e. to anything that imports sample.h). If you want the integer corresponding to Saturday, for example, it's just sa, not days.sa. I think you're confusing enums with structures.

在您创建的 enum 中sm等 现在在全球范围内可用(即对于任何 import sample.h)。例如,如果您想要与星期六对应的整数,则它只是sa,而不是days.sa。我认为您将枚举与结构混淆了。

For this reason, it's better to use more verbose names in your enum. Something like:

因此,最好在枚举中使用更详细的名称。就像是:

typedef enum
{
    WeekdaySunday = 1,
    WeekdayMonday,
    WeekdayTuesday,
    WeekdayWednesday,
    WeekdayThursday,
    WeekdayFriday,
    WeekdaySaturday
} Weekday;

so e.g. WeekdayMondayis now just another way of writing 2in your app, but will make your code more readable and pre-defines the possible legal values for a variable of type Weekday.

所以 egWeekdayMonday现在只是2在您的应用程序中编写的另一种方式,但将使您的代码更具可读性,并为类型的变量预定义可能的合法值Weekday

The above is fine, but for better compiler support and to ensure the size of a Weekday, I'd recommend using NS_ENUM:

以上很好,但为了更好的编译器支持并确保 a 的大小Weekday,我建议使用NS_ENUM

typedef NS_ENUM(NSInteger, Weekday)
{
    WeekdaySunday = 1,
    WeekdayMonday,
    WeekdayTuesday,
    WeekdayWednesday,
    WeekdayThursday,
    WeekdayFriday,
    WeekdaySaturday
};

回答by morroko

hey you use enum like this here is an example

嘿,你像这样使用枚举,这是一个例子

In .h define enum

在 .h 中定义枚举

typedef enum{s=1,m,t,w,th,f,sa} days;

In .m play with enum element like this

在 .m 中像这样玩 enum 元素

days d1 =f;



    switch (d1) {
        case m:
        case t:
            NSLog(@"You like Tuesday");
            break;
        case w:
        case th:

            break;
        case f:
            NSLog(@"You like friday");
            break;
        case sa:
            NSLog(@"You satureday");
            break;
        case s:
            NSLog(@"You like sunday");
            break;
        default:
            break;
    }

if you want learn more clickthis.

如果你想了解更多,请点击这里。

回答by Dasem

#import <Foundation/Foundation.h>

 typedef enum{
   s=1,m,t,w,th,f,sa
} days;

 @interface weekday : NSObject
 @property (nonatomic, assign) days day;
 @end

 @implementation weekday
 @end

 int main(int argc, const char * argv[])
 {

  @autoreleasepool {

    weekday *sunDay=[[weekday alloc]init];
    sunDay.day=s;
    NSLog(@"Today is %d",sunDay.day);

  }
return 0;
}

回答by AMohan

Creating Enum in Enumrations.h

在 Enumrations.h 中创建枚举

typedef enum  
{  
    Atype = 1,  
    Btype,  
    Ctype,
    Dtype,  
    Etype,  

}type;

Where ever you want to user this enum just import Enumrations.h, and you can use Atype without creating type object.

无论您想在何处使用此枚举,只需导入 Enumrations.h,您就可以在不创建类型对象的情况下使用 Atype。

you can simply use NSLog(@"%@",@(Atype)).

你可以简单地使用NSLog(@"%@",@(Atype)).