Typedef枚举设置和访问

sud*_*-rf 3 iphone enums typedef objective-c

我在MainView.h标题中的接口声明之前有这个.

typedef enum { UNKNOWN, CLEAR, NIGHT_CLEAR, CLOUDY, NIGHT_CLOUDY } Weather;
Run Code Online (Sandbox Code Playgroud)

然后我宣布它是这样的:

Weather weather;
Run Code Online (Sandbox Code Playgroud)

然后做了一个访问者:

@property Weather weather;
Run Code Online (Sandbox Code Playgroud)

并合成它.

我的问题是,如何在不崩溃的情况下在不同的类中使用它? 我已经为MainView导入了标题.我试着像这样使用它:

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

[myView setWeather: CLEAR];
Run Code Online (Sandbox Code Playgroud)

它不会在Xcode中引发任何错误,但在代码运行时它会崩溃,并说:

-[UIView setWeather:]: unrecognized selector sent to instance *blah*
Run Code Online (Sandbox Code Playgroud)

我在这里做错了吗?

wes*_*der 6

'天气'是一种不是变量的类型.

所以,你想要这样的东西:

Weather theWeather = [mainView weather];
if (theWeather == CLEAR)
{
<do something>
}
Run Code Online (Sandbox Code Playgroud)

MainView有ivar的地方:

 Weather weather;
Run Code Online (Sandbox Code Playgroud)