开发者

How do I use enumerated datatypes in Objective-C?

开发者 https://www.devze.com 2023-03-11 10:56 出处:网络
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 con

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开发者_StackOverflow中文版, kDirectionDown, etc.

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){})


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:

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 posterType = CFPosterViewTypeStart;

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

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.


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.

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.


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 }
0

精彩评论

暂无评论...
验证码 换一张
取 消