3

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?

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • Checking Weather, which looks like a type here, prolly wont work. Need to check the instance? – brumScouse Oct 26 '10 at 18:16
  • Thanks for replying. How would check the instance? – sudo rm -rf Oct 26 '10 at 18:23
  • 1
    In C, you would create a variable of the type Weather like so: Weather todaysWeather; - todaysWeather is the variable/ the instance of Weather and would be checked thus: if (todaysWeather == Weather.CLEAR) .... – brumScouse Oct 26 '10 at 18:26

2 Answers2

6

'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
  • 4,967
  • 5
  • 36
  • 51
  • 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. – sudo rm -rf Oct 26 '10 at 18:53
  • How would I access the `weather` variable outside of my class, though? – sudo rm -rf Oct 26 '10 at 19:02
  • 1
    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 Oct 26 '10 at 19:52
1

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

Rits
  • 5,105
  • 4
  • 42
  • 53