objective-c 在 Objective C 中使用枚举类型作为属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1627501/
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-03 22:24:58  来源:igfitidea点击:

Using enum types as properties in Objective C

objective-cpropertiesenums

提问by

I'm a veteran .NET developer making my first foray into Objective C programming. I'm having difficulty with a property of an enum type. Some context... I have an class header and enum like this:

我是一位资深的 .NET 开发人员,第一次涉足 Objective C 编程。我在使用枚举类型的属性时遇到困难。一些上下文......我有一个类头和枚举是这样的:

typedef enum  {
    Open,
    Unavailable,
    Unknown
} LocationStatus;

@interface Location : NSObject {

    LocationStatus status;
}

@property (nonatomic) LocationStatus status;

@end

and an implementation that looks like this:

和一个看起来像这样的实现:

@implementation Location

@synthesize status;

@end

At some point in the code, I'm setting the value like this:

在代码中的某个点,我设置的值是这样的:

location1.status = Open;

The debugger then evaluates this as having the correct value, and it is resolving to the correct enum (note also that there are other properties not shown here... they too evaluate properly).

然后调试器将其评估为具有正确的值,并且它正在解析为正确的枚举(另请注意,此处未显示其他属性......它们也正确评估)。

Later on in the code, I attempt to read that property like this:

稍后在代码中,我尝试像这样读取该属性:

LocationStatus status = location.status;

At this point in the code, the debugger is able to evaluate all the properties of my class correctly, except Status, which shows a memory address, but not an actual value. When the execution reaches this line, I consistently get a EXC_BAD_ACCESS error in the console, and the app crashes.

在代码的这一点上,调试器能够正确评估我的类的所有属性,除了Status,它显示内存地址,但不是实际值。当执行到达这一行时,我一直在控制台中收到 EXC_BAD_ACCESS 错误,并且应用程序崩溃。

I'm pretty sure this reflects a fundamental misunderstanding on my part on how to use properties and enums in Objective C (and probably C in general). If anyone could shed some light on this, I'd be most grateful.

我很确定这反映了我对如何在 Objective C(通常也可能是 C)中使用属性和枚举的基本误解。如果有人能对此有所了解,我将不胜感激。

回答by sc0rp10n

It might be too late to answer this but I did notice one thing in your code. You are using 2 different variables in you code location1 and location (without the 1).

回答这个可能为时已晚,但我确实注意到您的代码中的一件事。您在代码 location1 和 location(没有 1)中使用了 2 个不同的变量。

EXEC_BAD_ACCESSgenerally means that you are trying to send a message to an object that does not exist. Usually this is because it has been deallocated. However, in your case it appears that it never existed in the first place.

EXEC_BAD_ACCESS通常意味着您正在尝试向不存在的对象发送消息。通常这是因为它已被释放。但是,在您的情况下,它似乎一开始就不存在。

As you noted you don't allocate an enum. But its not the enum that is the problem. The "dot" syntax in objective-c is just a short cut for sending an accessor message.

正如您所指出的,您没有分配枚举。但问题不是枚举。Objective-c 中的“点”语法只是发送访问者消息的捷径。

Your code is equivalent to:

您的代码相当于:

LocationStatus status = [location status];

That sends the synthesized -(LocationStatus)status{}message to the non-existent location object (unless of course location1was just a typo in your post, but not in your code, which makes my comment irrelevant). So just change location.statusto location1.statusand you should be good to go (unless, of course, location1is being released before you send the message to it).

这会将合成的-(LocationStatus)status{}消息发送到不存在的位置对象(当然location1,除非您的帖子中只是一个错字,而不是您的代码中的错字,这使得我的评论无关紧要)。因此,只需更改location.statuslocation1.status,您就可以开始使用了(当然,除非location1在您向其发送消息之前已被释放)。

回答by Sixten Otto

EXC_BAD_ACCESS almost always means you're trying to use a reference to an object that's been deallocated (usually an over-release bug). Search for that error here on SO to find lotsof advice on tracking it down.

EXC_BAD_ACCESS 几乎总是意味着您正在尝试使用对已释放对象的引用(通常是过度释放错误)。在 SO 上搜索该错误,以找到有关跟踪它的大量建议。