xcode typedef enum:哪些类可以访问它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6011329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:02:04  来源:igfitidea点击:

typedef enum: which classes have access to it

iphoneobjective-cxcodeenumstypedef

提问by Josh Sherick

NOTE: i changed the names, i dont actually have a type letters.

注意:我更改了名称,实际上我没有类型字母。

in my root view controller .h, i do

在我的根视图控制器 .h 中,我做

typedef enum {
a,
b,
c,
d,
e }letters;

i have several other classes, most of which need to use this "letters" type. in my root view controller, i use "#import "MyClass.h" to import the class (since i make an instance of it in rootViewController). however, this does not allow MyClass to use the type "letters".

我还有其他几个类,其中大部分需要使用这种“字母”类型。在我的根视图控制器中,我使用“#import”MyClass.h”来导入类(因为我在 rootViewController 中创建了它的一个实例)。但是,这不允许 MyClass 使用“字母”类型。

i tried adding in MyClass.h, "#import rootViewController.h", but xcode started giving me errors (only sometimes, it was on and off). what is the best way to do this? should i just import the rootViewController to all of the classes that need this "letters" type? is it ok to have both rootViewController importing MyClass and MyClass importing rootViewController?

我尝试在 MyClass.h 中添加“#import rootViewController.h”,但 xcode 开始给我错误(只是有时,它是打开和关闭的)。做这个的最好方式是什么?我应该将 rootViewController 导入到所有需要这种“字母”类型的类吗?让 rootViewController 导入 MyClass 和 MyClass 导入 rootViewController 可以吗?

回答by Anomie

If you need to use the enum only in connection with one class, go ahead and include it in that class's .h file. If you need to use the enum in many different places, you would probably be better served by creating a .h file just to declare the enum, and include that wherever you need it.

如果您只需要在一个类中使用枚举,请继续并将其包含在该类的 .h 文件中。如果您需要在许多不同的地方使用枚举,那么创建一个 .h 文件来声明枚举可能会更好,并在需要的地方包含它。

Your errors were probably coming from missing forward declarations. If RootViewController has ivars, properties, or method signatures that refer to MyClass, then there needs to be either a @class MyClass;or an @interface block for MyClass visible before the compiler gets to those ivars, properties, or method signatures. If RootViewController.h and MyClass.h both include the other, then it will work if RootViewController.h is imported first but fail if MyClass.h is imported first.

您的错误可能来自缺少前向声明。如果 RootViewController 具有引用 MyClass 的 ivars、属性或方法签名,那么@class MyClass;在编译器获取这些 ivars、属性或方法签名之前,需要有一个MyClass 可见的或 @interface 块。如果 RootViewController.h 和 MyClass.h 都包含另一个,那么如果首先导入 RootViewController.h 它将工作但如果首先导入 MyClass.h 则失败。

回答by bbum

A typedef enumis kinda like a #definewith a bit of error checking (limited to integral types).

Atypedef enum有点像#define带有一些错误检查的 a(仅限于整数类型)。

I.e. about the only way it can cause a problem is if you screw up the syntax or declare something with the same name (a re-declaration error).

即,它可能导致问题的唯一方法是,如果您搞砸了语法或声明了具有相同名称的内容(重新声明错误)。

Outside of that, unlikely to be your problem.

除此之外,不太可能是你的问题。

Impossible to say more without seeing the error messages.

如果没有看到错误消息,就无法多说。