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

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

问题描述

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



在直线C中,我只需要 #include 另一个头文件,但在Objective-C中,建议不要在头文件之间使用 #import ,而是使用forward @class 需要声明。但是,我不知道如何转发 - 声明枚举类型。



我不需要实际的枚举值,除了相应的 .m 实现文件,我可以安全地 #import 。那么如何才能在标题中得到 typedef枚举

解决方案

继续使用 #import 。人们建议尽可能使用 @class 的唯一原因是因为它使您的代码编译速度稍快。但是,从另一个 #import 将一个.h文件没有问题。事实上,你需要在扩展另一个类时执行此操作。


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.

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.

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?

解决方案

Go ahead and use #import. The only reason people recommend to use @class when possible is because it makes your code slightly faster to compile. However, there is no issue with #importing one .h file from another. In fact, you need to do this when extending another class.

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

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