如何将类属性声明为枚举类型

时间:2021-04-06 20:53:46

I want to declare a custom enum such as:

我想声明一个自定义枚举,例如:

enum menuItemType
{
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
}

As a type for my object:

作为我的对象的类型:

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    enum menuItemType *menuType;
    NSMutableArray *subMenuItems;
}

But am unsure where I need to put the enum definition - if I put it before the @interface its syntactically incorrect

但我不确定我需要在哪里放置枚举定义 - 如果我把它放在@interface之前它的语法不正确

3 个解决方案

#1


10  

If you put your two code snippets in a file in this order, you just have to add a ; at the end of the enum declaration and you may want to consider using an enum variable instead of a pointer to an enum variable.

如果按此顺序将两个代码段放在一个文件中,则只需添加一个;在枚举声明结束时,您可能需要考虑使用枚举变量而不是指向枚举变量的指针。

This gives:

这给出了:

enum menuItemType
{
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
};

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    enum menuItemType menuType;
    NSMutableArray *subMenuItems;
}

#2


15  

Just updating this in case someone stumbles upon this in our future times.

只是更新这个以防万一有人在我们未来的时代偶然发现这一点。

Since iOS 6 / Mac OS X 10.8, the new NS_ENUM and NS_OPTIONS macros are the preferred way to declare enum types.

从iOS 6 / Mac OS X 10.8开始,新的NS_ENUM和NS_OPTIONS宏是声明枚举类型的首选方式。

In that case, it would look like:

在这种情况下,它看起来像:

typedef NS_ENUM(NSInteger, MenuItemType) {
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
};

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    MenuItemType menuType;
    NSMutableArray *subMenuItems;
}

A good read on the subject: http://nshipster.com/ns_enum-ns_options/

关于这个主题的一个很好的阅读:http://nshipster.com/ns_enum-ns_options/

You might also want to conform to Apple's conventions for enum naming and go for something like:

您可能还希望符合Apple的enum命名约定,并执行以下操作:

typedef NS_ENUM(NSInteger, MenuItemType) {
    MenuItemTypeLinkInternal = 0,
    MenuItemTypeLinkExternal = 1,
    MenuItemTypeImage = 2,
    MenuItemTypeMovie = 3,
    MenuItemTypeMapQuery = 4
};

Hope this will help.

希望这会有所帮助。

#3


5  

@mouviciel is right, but I thought I'd let you know that what you want is not a class property, something not supported in Objective-C. A class property is actually a global property that is set on the class-object. What you were thinking of was a plain-old property (set on an instance of the class).

@mouviciel是对的,但我想我会告诉你,你想要的不是类属性,Objective-C不支持。类属性实际上是在类对象上设置的全局属性。你在想的是一个普通的属性(设置在类的实例上)。

Also, what your code shows is that you are just using an instance variable. Instance variables can be turned into properties by adding accessor/mutator methods as follows:

此外,您的代码显示的是您只是使用实例变量。可以通过添加accessor / mutator方法将实例变量转换为属性,如下所示:

// after @interface {}
@property (readwrite) enum menuItemType menuType;

and

// under @implementation
@synthesize menuType;

This is a shortcut: the compiler will generate the proper methods to access and change the menuType property. I'm not sure how useful all this is to you, but it will help you with your understanding of Objective-C semantics.

这是一个快捷方式:编译器将生成访问和更改menuType属性的正确方法。我不确定这对你有多大帮助,但它会帮助你理解Objective-C语义。

#1


10  

If you put your two code snippets in a file in this order, you just have to add a ; at the end of the enum declaration and you may want to consider using an enum variable instead of a pointer to an enum variable.

如果按此顺序将两个代码段放在一个文件中,则只需添加一个;在枚举声明结束时,您可能需要考虑使用枚举变量而不是指向枚举变量的指针。

This gives:

这给出了:

enum menuItemType
{
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
};

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    enum menuItemType menuType;
    NSMutableArray *subMenuItems;
}

#2


15  

Just updating this in case someone stumbles upon this in our future times.

只是更新这个以防万一有人在我们未来的时代偶然发现这一点。

Since iOS 6 / Mac OS X 10.8, the new NS_ENUM and NS_OPTIONS macros are the preferred way to declare enum types.

从iOS 6 / Mac OS X 10.8开始,新的NS_ENUM和NS_OPTIONS宏是声明枚举类型的首选方式。

In that case, it would look like:

在这种情况下,它看起来像:

typedef NS_ENUM(NSInteger, MenuItemType) {
    LinkInternal = 0,
    LinkExternal = 1,
    Image = 2,
    Movie = 3,
    MapQuery = 4
};

@interface MenuItem : NSObject {    
    NSMutableString *menuId;
    NSMutableString *title;
    MenuItemType menuType;
    NSMutableArray *subMenuItems;
}

A good read on the subject: http://nshipster.com/ns_enum-ns_options/

关于这个主题的一个很好的阅读:http://nshipster.com/ns_enum-ns_options/

You might also want to conform to Apple's conventions for enum naming and go for something like:

您可能还希望符合Apple的enum命名约定,并执行以下操作:

typedef NS_ENUM(NSInteger, MenuItemType) {
    MenuItemTypeLinkInternal = 0,
    MenuItemTypeLinkExternal = 1,
    MenuItemTypeImage = 2,
    MenuItemTypeMovie = 3,
    MenuItemTypeMapQuery = 4
};

Hope this will help.

希望这会有所帮助。

#3


5  

@mouviciel is right, but I thought I'd let you know that what you want is not a class property, something not supported in Objective-C. A class property is actually a global property that is set on the class-object. What you were thinking of was a plain-old property (set on an instance of the class).

@mouviciel是对的,但我想我会告诉你,你想要的不是类属性,Objective-C不支持。类属性实际上是在类对象上设置的全局属性。你在想的是一个普通的属性(设置在类的实例上)。

Also, what your code shows is that you are just using an instance variable. Instance variables can be turned into properties by adding accessor/mutator methods as follows:

此外,您的代码显示的是您只是使用实例变量。可以通过添加accessor / mutator方法将实例变量转换为属性,如下所示:

// after @interface {}
@property (readwrite) enum menuItemType menuType;

and

// under @implementation
@synthesize menuType;

This is a shortcut: the compiler will generate the proper methods to access and change the menuType property. I'm not sure how useful all this is to you, but it will help you with your understanding of Objective-C semantics.

这是一个快捷方式:编译器将生成访问和更改menuType属性的正确方法。我不确定这对你有多大帮助,但它会帮助你理解Objective-C语义。