如何在Objective-C中使用枚举数据类型?

时间:2022-09-07 08:22:04

I'm working on several iOS projects where I think enumerated datatypes would be helpful to me. For example, I have a game where the player can walk in several directions. I could just define four constants with string values as kDirectionUp, kDirectionDown, etc.

我正在研究几个iOS项目,我认为枚举数据类型对我有帮助。例如,我有一个游戏,玩家可以在几个方向走路。我可以用字符串值定义四个常量,如kDirectionUp,kDirectionDown等。

I think an enumerated type would be better here. Is that correct? If so, how do I define an enum here so that I can later compare values? (As in, if(someValue == kDirectionUp){})

我认为枚举类型在这里会更好。那是对的吗?如果是这样,我如何在这里定义一个枚举,以便我以后可以比较值? (如,if(someValue == kDirectionUp){})

3 个解决方案

#1


13  

That sounds like the right thing to do.

这听起来是正确的事情。

It's really simple to create enums in Objective-C using C-style type definitions. For example, in one of my header files, I have the following type definition:

使用C风格的类型定义在Objective-C中创建枚举非常简单。例如,在我的一个头文件中,我有以下类型定义:

typedef enum {
  CFPosterViewTypePoster = 0,
  CFPosterViewTypeStart, // 1
  CFPosterViewTypeEnd,   // 2
  ....                   // 3
} CFPosterViewType;   

You define an object of CFPosterViewType and set it to one of the values:

您定义CFPosterViewType的对象并将其设置为以下值之一:

CFPosterViewType posterType = CFPosterViewTypeStart;

When comparing CFPosterViewType values, it's as simple as doing the following:

比较CFPosterViewType值时,它就像执行以下操作一样简单:

if (posterType == CFPosterViewTypePoster) {
    // do something
}

Note that the commented out numbers in the enum above are implicit values. If you want to do something differently, say, define a bitmask, or anything else where you need the values to be different than the default, you'll need to explicitly define them.

请注意,上面枚举中注释掉的数字是隐含值。如果你想做一些不同的事情,比如说,定义一个位掩码,或者你需要的值与默认值不同的任何其他东西,你需要明确定义它们。

#2


5  

In a header file, define an enum type, e.g.:

在头文件中,定义枚举类型,例如:

// SomeHeaderFile.h
typedef enum {
    MOPlayerDirectionNone,
    MOPlayerDirectionUp,
    MOPlayerDirectionDown,
    …
} MOPlayerDirection;

Whenever you need to use MOPlayerDirection, #import that header file. You can then use it as a type as well as its possible values.

每当你需要使用MOPlayerDirection时,#import进入头文件。然后,您可以将其用作类型及其可能的值。

For instance:

例如:

#import "SomeHeaderFile.h"

@interface MOPlayer : NSObject {
    MOPlayerDirection currentDirection;
}

- (void)moveToDirection:(MOPlayerDirection)direction;
- (void)halt;
@end

and:

和:

#import "SomeHeaderFile.h"
#import "MOPlayer.h"

@implementation MOPlayer

- (id)init {
    self = [super init];
    if (self) {
        currentDirection = MOPlayerDirectionNone;
    }
    return self;
}

- (void)moveToDirection:(MOPlayerDirection)direction {
    currentDirection = direction;

    switch (currentDirection) {
        case MOPlayerDirectionUp:
            // do something
            break;

        case MOPlayerDirectionDown:
            // do something
            break;
    }
}

- (void)halt {
    if (currentDirection != MOPlayerDirectionNone) {
        // do something
        currentDirection = MOPlayerDirectionNone;
    }
}

@end

If an enumeration is tightly related to a class, it’s common to define it in the same header file as the class declaration. In the example above, instead of defining MOPlayerDirection in SomeHeaderFile.h, you could define it in MOPlayer.h instead.

如果枚举与类紧密相关,则通常在与类声明相同的头文件中定义它。在上面的示例中,您可以在MOPlayer.h中定义MOPlayerDirection,而不是在SomeHeaderFile.h中定义MOPlayerDirection。

#3


2  

Just define them at the top of your file:

只需在文件顶部定义它们:

enum    // direction types
{
    kDirectionUp = 0,
    kDirectionDown, // 1
    kDirectionLeft, // 2
    kDirectionRight // 3

};

then you can call as required:

然后你可以按要求打电话:

if(someValue == kDirectionUp){ // do something }

#1


13  

That sounds like the right thing to do.

这听起来是正确的事情。

It's really simple to create enums in Objective-C using C-style type definitions. For example, in one of my header files, I have the following type definition:

使用C风格的类型定义在Objective-C中创建枚举非常简单。例如,在我的一个头文件中,我有以下类型定义:

typedef enum {
  CFPosterViewTypePoster = 0,
  CFPosterViewTypeStart, // 1
  CFPosterViewTypeEnd,   // 2
  ....                   // 3
} CFPosterViewType;   

You define an object of CFPosterViewType and set it to one of the values:

您定义CFPosterViewType的对象并将其设置为以下值之一:

CFPosterViewType posterType = CFPosterViewTypeStart;

When comparing CFPosterViewType values, it's as simple as doing the following:

比较CFPosterViewType值时,它就像执行以下操作一样简单:

if (posterType == CFPosterViewTypePoster) {
    // do something
}

Note that the commented out numbers in the enum above are implicit values. If you want to do something differently, say, define a bitmask, or anything else where you need the values to be different than the default, you'll need to explicitly define them.

请注意,上面枚举中注释掉的数字是隐含值。如果你想做一些不同的事情,比如说,定义一个位掩码,或者你需要的值与默认值不同的任何其他东西,你需要明确定义它们。

#2


5  

In a header file, define an enum type, e.g.:

在头文件中,定义枚举类型,例如:

// SomeHeaderFile.h
typedef enum {
    MOPlayerDirectionNone,
    MOPlayerDirectionUp,
    MOPlayerDirectionDown,
    …
} MOPlayerDirection;

Whenever you need to use MOPlayerDirection, #import that header file. You can then use it as a type as well as its possible values.

每当你需要使用MOPlayerDirection时,#import进入头文件。然后,您可以将其用作类型及其可能的值。

For instance:

例如:

#import "SomeHeaderFile.h"

@interface MOPlayer : NSObject {
    MOPlayerDirection currentDirection;
}

- (void)moveToDirection:(MOPlayerDirection)direction;
- (void)halt;
@end

and:

和:

#import "SomeHeaderFile.h"
#import "MOPlayer.h"

@implementation MOPlayer

- (id)init {
    self = [super init];
    if (self) {
        currentDirection = MOPlayerDirectionNone;
    }
    return self;
}

- (void)moveToDirection:(MOPlayerDirection)direction {
    currentDirection = direction;

    switch (currentDirection) {
        case MOPlayerDirectionUp:
            // do something
            break;

        case MOPlayerDirectionDown:
            // do something
            break;
    }
}

- (void)halt {
    if (currentDirection != MOPlayerDirectionNone) {
        // do something
        currentDirection = MOPlayerDirectionNone;
    }
}

@end

If an enumeration is tightly related to a class, it’s common to define it in the same header file as the class declaration. In the example above, instead of defining MOPlayerDirection in SomeHeaderFile.h, you could define it in MOPlayer.h instead.

如果枚举与类紧密相关,则通常在与类声明相同的头文件中定义它。在上面的示例中,您可以在MOPlayer.h中定义MOPlayerDirection,而不是在SomeHeaderFile.h中定义MOPlayerDirection。

#3


2  

Just define them at the top of your file:

只需在文件顶部定义它们:

enum    // direction types
{
    kDirectionUp = 0,
    kDirectionDown, // 1
    kDirectionLeft, // 2
    kDirectionRight // 3

};

then you can call as required:

然后你可以按要求打电话:

if(someValue == kDirectionUp){ // do something }