开发者

where does a typedef enum statement go in Objective-C?

A basic question I fear. The following code works, and the typedef enumeration is recognised, but I get a warning message "useless storage class specifier in empty declaration". Am I doing something wrong here and is this the best place to put a typedef enum?

#import <UIKit/UIKit.h>
#import "CoreDataBaseTableViewController.h"

typede开发者_开发问答f enum ColourType {
    BACKGROUND=1,
    LOW=2,
    HIGH=3,
    EXTRA=4
};

@interface ColourList : CoreDataBaseTableViewController <NSFetchedResultsControllerDelegate> {

    NSManagedObjectContext* moc;    
    NSFetchedResultsController* fetchedResultsController; 

    ...

    enum ColourType colourTarget;

}

...


You can put an enumeration anywhere in Objective-C which is valid in C. Where you have it now (above the interface) is a common place for enumerations which should be globally available. The warning is because you are using typedef, but don't actually define a type. If you simply want to create an enumeration, it isn't necessary. You just use:

enum ColourType {
    BACKGROUND=1,
    LOW=2,
    HIGH=3,
    EXTRA=4
};

You use typedef to define a type, which makes it easier to reference commonly used structures/unions/enumerations/other types. If you choose to do this, you should place a name for the type after the enumeration definition, and then you can reference the enumeration by using that name without the enum keyword.

typedef enum ColourType {
    BACKGROUND=1,
    LOW=2,
    HIGH=3,
    EXTRA=4
} MyColourType;
MyColourType colour;

Alternatively, you can create the enumeration and type in separate commands with the same effect.

enum ColourType {
    BACKGROUND=1,
    LOW=2,
    HIGH=3,
    EXTRA=4
};
typedef enum ColourType MyColourType;


By the by, consider using Objective-C's NS_ENUM macro. Take it away, Apple Docs
::polite applause as AD takes the mic::

The NS_ENUM and NS_OPTIONS macros provide a concise, simple way of defining enumerations and options in C-based languages. These macros improve code completion in Xcode and explicitly specify the type and size of your enumerations and options. Additionally, this syntax declares enums in a way that is evaluated correctly by older compilers, and by newer ones that can interpret the underlying type information.

Syntax example:

  typedef NS_ENUM(NSInteger, DRWColourType) {
  DRWColourTypeBackground,
  DRWColourTypeLow,
  DRWColourTypeHigh,
  DRWColourTypeExtra
};

Why? NSHipster:

This approach combines the best of all of the aforementioned approaches, and even provides hints to the compiler for type-checking and switch statement completeness.


Either get rid of typedef, or provide an alias for the type:

typedef enum X {...} Y;


Yes it will work just fine since objective c is just a superset of C. You must provide an alias for your enum like so:

typedef enum ColourType {
    BACKGROUND=1,
    LOW=2,
    HIGH=3,
    EXTRA=4
} MyColourType;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜