简体   繁体   English

使用枚举类型作为Objective C中的属性

[英]Using enum types as properties in Objective C

I'm a veteran .NET developer making my first foray into Objective C programming. 我是一位资深的.NET开发人员,他第一次涉足Objective C编程。 I'm having difficulty with a property of an enum type. 我对枚举类型的属性有困难。 Some context... I have an class header and enum like this: 一些上下文...我有一个类头和枚举像这样:

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. 在代码的这一点上,调试器能够正确地评估我的类的所有属性,除了Status ,它显示内存地址,但不是实际值。 When the execution reaches this line, I consistently get a EXC_BAD_ACCESS error in the console, and the app crashes. 当执行到达此行时,我一直在控制台中收到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). 我很确定这反映了我对如何在Objective C中使用属性和枚举(以及可能是C)的基本误解。 If anyone could shed some light on this, I'd be most grateful. 如果有人能对此有所了解,我将非常感激。

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_ACCESS generally means that you are trying to send a message to an object that does not exist. EXEC_BAD_ACCESS通常表示您尝试将消息发送到不存在的对象。 Usually this is because it has been deallocated. 通常这是因为它已被解除分配。 However, in your case it appears that it never existed in the first place. 但是,在您的情况下,它似乎从未存在过。

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 location1 was just a typo in your post, but not in your code, which makes my comment irrelevant). 这将合成的-(LocationStatus)status{}消息发送到不存在的位置对象(除非当然, location1只是您帖子中的拼写错误,但不在您的代码中,这使得我的评论无关紧要)。 So just change location.status to location1.status and you should be good to go (unless, of course, location1 is being released before you send the message to it). 所以只需将location.status更改为location1.status ,你应该好好去(当然,除非在你向它发送消息之前发布了location1 )。

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). EXC_BAD_ACCESS几乎总是意味着您正在尝试使用对已被解除分配的对象的引用(通常是过度释放的错误)。 Search for that error here on SO to find lots of advice on tracking it down. 在SO上搜索该错误,以找到有关跟踪它的大量建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM