Using enum types as properties in Objective 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.

Answers


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


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.


Need Your Help

Cryptography - RSA Algorithm in Java 1.4

java encryption cryptography rsa java1.4

I am using Java 1.4.2_10 and I am trying to use RSA encryption:

Move a gameObject to another object after entering collision. Unity C#

c# unity3d unity5

I am very beginner in C# and Unity3D, so my question might be weird but please don't judge me. I have a trouble to figuring out what's wrong and why it doesn't work.