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)的基本误解。如果有人能够对此有所了解,我将非常感激。
2 个解决方案
#1
7
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. 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中的“dot”语法只是发送访问者消息的捷径。
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).
这将合成的 - (LocationStatus)状态{}消息发送到不存在的位置对象(除非当然,location1只是您帖子中的拼写错误,但不在您的代码中,这使得我的评论无关紧要)。所以只需将location.status更改为location1.status,你就应该好了(当然,除非在你向它发送消息之前发布了location1)。
#2
5
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.
EXC_BAD_ACCESS几乎总是意味着您正在尝试使用对已释放的对象的引用(通常是过度释放的错误)。在SO上搜索该错误,以找到有关跟踪它的大量建议。
#1
7
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. 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中的“dot”语法只是发送访问者消息的捷径。
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).
这将合成的 - (LocationStatus)状态{}消息发送到不存在的位置对象(除非当然,location1只是您帖子中的拼写错误,但不在您的代码中,这使得我的评论无关紧要)。所以只需将location.status更改为location1.status,你就应该好了(当然,除非在你向它发送消息之前发布了location1)。
#2
5
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.
EXC_BAD_ACCESS几乎总是意味着您正在尝试使用对已释放的对象的引用(通常是过度释放的错误)。在SO上搜索该错误,以找到有关跟踪它的大量建议。