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

发布于 2024-08-09 04:18:50 字数 847 浏览 4 评论 0原文

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

typedef enum  {
    Open,
    Unavailable,
    Unknown
} LocationStatus;

@interface Location : NSObject {

    LocationStatus status;
}

@property (nonatomic) LocationStatus status;

@end

以及一个如下所示的实现:

@implementation Location

@synthesize status;

@end

在代码中的某个时刻,我设置如下值:

location1.status = Open;

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

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

LocationStatus status = location.status;

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

我很确定这反映了我对如何在 Objective C(可能还有一般的 C)中使用属性和枚举的根本误解。如果有人能阐明这一点,我将不胜感激。

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:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-08-16 04:18:50

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

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

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

您的代码相当于:

LocationStatus status = [location status];

将合成的 -(LocationStatus)status{} 消息发送到不存在的位置对象(当然,除非 location1 只是您的拼写错误)发布,但不在您的代码中,这使得我的评论无关紧要)。因此,只需将 location.status 更改为 location1.status 就可以了(当然,除非 location1 在您之前发布)向其发送消息)。

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).

EXEC_BAD_ACCESS generally 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.

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.

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). 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).

℉絮湮 2024-08-16 04:18:50

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

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 lots of advice on tracking it down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文