Objective-C 中的前向声明枚举 [英] Forward-declare enum in Objective-C

查看:40
本文介绍了Objective-C 中的前向声明枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Objective-C 程序中遇到枚举可见性的问题.我有两个头文件,一个定义了 typedef enum.另一个文件需要使用 typedef 的类型.

I'm having trouble with enum visibility in an Objective-C program. I have two header files, and one defines a typedef enum. Another file needs to use the typedef'd type.

在直接的 C 中,我会简单地 #include 另一个头文件,但在 Objective-C 中,建议不要在头文件之间使用 #import,而是使用根据需要转发 @class 声明.但是,我不知道如何向前声明枚举类型.

In straight C, I would simply #include the other header file, but in Objective-C, it's recommended not to use #import between header files, instead using forward @class declarations as needed. However, I can't figure out how to forward-declare an enumeration type.

我不需要实际的枚举值,除了在相应的.m 实现文件中,我可以安全地#import 离开.那么如何才能在标题中识别 typedef enum 呢?

I don't need the actual enumerated values, except in the corresponding .m implementation file, where I can safely #import away. So how can I get the typedef enum to be recognized in the header?

推荐答案

在 Objective-c 中转发声明枚举 (NS_ENUM/NS_OPTION) 的最新方法(Swift 3;2017 年 5 月)是使用以下内容:

Most recent way (Swift 3; May 2017) to forward declare the enum (NS_ENUM/NS_OPTION) in objective-c is to use the following:

// Forward declaration for XYZCharacterType in other header say XYZCharacter.h
typedef NS_ENUM(NSUInteger, XYZCharacterType);


// Enum declaration header: "XYZEnumType.h"
#ifndef XYZCharacterType_h
#define XYZCharacterType_h

typedef NS_ENUM(NSUInteger, XYZEnumType) {
    XYZCharacterTypeNotSet,
    XYZCharacterTypeAgent,
    XYZCharacterTypeKiller,
};

#endif /* XYZCharacterType_h */`

这篇关于Objective-C 中的前向声明枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆