本文介绍了typedef枚举语句在Objective-C中哪里去了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心的一个基本问题.以下代码可以正常工作,并且可以识别typedef枚举,但是我收到一条警告消息空声明中的无用存储类说明符".我在这里做错什么了吗,这是放置typedef枚举的最佳位置吗?

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"

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

@interface ColourList : CoreDataBaseTableViewController <NSFetchedResultsControllerDelegate> {

    NSManagedObjectContext* moc;
    NSFetchedResultsController* fetchedResultsController;

    ...

    enum ColourType colourTarget;

}

...

推荐答案

您可以将枚举放置在Objective-C中的任何位置,该位置在C中是有效的.现在(在界面上方)拥有枚举的位置通常是枚举的地方,应该是全球可用的.警告是因为您使用的是 typedef ,但实际上并未定义类型.如果您只想创建一个枚举,则没有必要.您只需使用:

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

使用 typedef 定义类型,这使得引用常用的结构/联合/枚举/其他类型更加容易.如果选择执行此操作,则应在枚举定义之后放置该类型的名称,然后可以使用该名称而不使用 enum 关键字来引用该枚举.

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;

这篇关于typedef枚举语句在Objective-C中哪里去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:55