3

How can I assign an to a variable and access its value later? I thought this would be pretty simple, but every time I try to assign the enum value to a variable (no type mismatches or warnings in Xcode appear) my app crashes with an EXC_BAD_ACCESS error.

Here's how I setup my enum in my header file (BarTypes.h):

typedef enum {
    BarStyleGlossy,
    BarStyleMatte,
    BarStyleFlat
} BarDisplayStyle;

No issues there (reading and using the values at least). However, when I create a variable that can store one of the enum values (BarStyleGlossy, BarStyleMatte, or BarStyleFlat) then try to set that variable, the app crashes. Here's how I setup and use the variable:

//Header
@property (nonatomic, assign, readwrite) BarDisplayStyle barViewDisplayStyle; //I've also tried just using (nonatomic) and I've also tried (nonatomic, assign)

//Implementation
@synthesize barViewDisplayStyle;

- (void)setupBarStyle:(BarDisplayStyle)displayStyle {
    //This is where it crashes:
    self.barViewDisplayStyle = displayStyle;
}

Why is it crashing here? How do I store the value of an enum in a variable? I think the issue has to do with a lack of understanding about enums on my end, however if I follow conventional variable setup and allocation, etc. this should work. Any ideas on what I'm doing wrong?

Please note that I'm new to enums, so my vocabulary here may be a bit mixed up (forgive me - and feel free to make an edit if you know what I'm trying to say).

I found a few references about enums across the web:

EDIT: Here's how I call the setupBarStyle method:

BarView *bar = [[BarView alloc] init];
[bar setupBarStyle:displayStyle];
3
  • show the call to the setupBarStyle method Commented Jun 5, 2013 at 20:57
  • @Wain Sure will! I made an edit to my question (edit #1) Commented Jun 5, 2013 at 21:00
  • 1
    Compiled and ran your code without any crash. Please add a crash log. Commented Jun 5, 2013 at 21:24

3 Answers 3

10

Just in case anyone out there is still trying to figure out how to assign an enum value to an enum typed variable or property... Here is an example using a property.

In the header file...

@interface elmTaskMeasurement : NSObject

typedef NS_ENUM(NSInteger, MeasurementType) {
    D,
    N,
    T,
    Y,
    M
};

@property(nonatomic) MeasurementType MeasureType;

@end

In the file where the object is created...

elmTaskMeasurement *taskMeasurement = [[elmTaskMeasurement alloc] init];

taskMeasurement.MeasureType = (MeasurementType)N;
Sign up to request clarification or add additional context in comments.

Comments

0

The method you implement is called setupBarStyle:, but you call setupBarShape: on the object.

2 Comments

Sorry, I made a typo transferring the method call to my question. I've fixed it now
@RazorSharp (I always wonder how possibly copy-paste can cause such typos...)
0

I had this error myself but the error was caused by a different bug I off course create myself.

The setter of my property "myApplicationState" was as follows:

-(void)setApplicationStyle:(myApplicationStyle)applicationStyle{
    self.applicationStyle = applicationStyle;
    //some more code
}

Off course this would result in an endless loop because in the setter, the setting is called again, and again, and again.

It had to be:

-(void)setApplicationStyle:(myApplicationStyle)applicationStyle{
    _applicationStyle = applicationStyle;
    //some more code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.