views:

48

answers:

4

Hello there!

I have this before the interface declaration in my MainView.h header.

typedef enum { UNKNOWN, CLEAR, NIGHT_CLEAR, CLOUDY, NIGHT_CLOUDY } Weather;

Then I declared it like this:

Weather weather;

Then made an accessor:

@property Weather weather;

And synthesized it.

My question is, how can I use this in a different class without it crashing? I've imported the header for MainView. I tried to use it like this:

MainView* myView = (MainView*)self.view;

[myView setWeather: CLEAR];

It doesn't throw me any errors in Xcode, but it crashes when the code is run, saying:

-[UIView setWeather:]: unrecognized selector sent to instance *blah*

Am I doing something wrong here?

A: 

In C, Weather would be a type definition, not a variable.

Look at typedef enum in Objective C

aberta
+2  A: 

'Weather' is a type not a variable.

So, you want something like this:

Weather theWeather = [mainView weather];
if (theWeather == CLEAR)
{
<do something>
}

Where MainView has ivar:

 Weather weather;
westsider
That was the correct way to do it, but when declaring it I didn't do it the way you wrote (although I'm sure it would work), but I instead put it in my interface and then made it a property. Then I could use it as a variable.
willingfamily
How would I access the `weather` variable outside of my class, though?
willingfamily
Typically, I define enums and #defines in a .h file that is then included in all my other .h files. Of course there are exceptions. That said, your property 'weather' should be accessible via getter/setter from anywhere that has access to an instance of your class. That said, the capitalization of 'MainView' in your code example causes me concern; that is the name of your class. An instance of MainView should be called something else, like mainView. Further, there is a good chance that 'weather' should be stored somewhere other than MainView, but that's neither here nor there.
westsider
A: 

Thanks for the help, but the actual problem here was that I was declaring my weather as a pointer, not as a variable. The declaration should have been:

Weather weather;

Then I could use that variable with:

if (weather == CLEAR) {}

But the crashing problem is still not fixed.

willingfamily
+1  A: 

You have to remove the * in Weather* weather. weather has to be an integer, not a pointer.

Rits
That was precisely the problem in my declaration.
willingfamily