Xcode 枚举问题

发布于 2024-09-05 17:53:36 字数 1409 浏览 1 评论 0原文

我以为我已经解决了这个问题,但我仍然缺少一些东西。

非常简单,我有一个包含 DAO(位于 plist 上)的 Settings 类。为了方便和可读性,我想要几个枚举来进行设置,例如 GamePlayType 和 DifficultyLevel。现在,我在 @interface 行上方的 Settings.h 文件中定义它们:

typedef enum {
 EASY,
 NORMAL,
 HARD
} DifficultyLevel;

如果

typedef enum {
 SET_NUMBER_OF_MOVES,
 TO_COMPLETION
} GamePlayType;

我从 Settings 类中访问它们,例如:

- (int)gridSizeForLOD {
 switch ([self difficultyLevel]) {
  case EASY:
   return GRID_SIZE_EASY;
  case NORMAL:
   return GRID_SIZE_NORMAL;
  case HARD:
   return GRID_SIZE_HARD;
  default:
   return GRID_SIZE_NORMAL;
 }
}

一切都很好。

但是,如果我尝试在 Settings 类之外访问它们,比如说在我的主视图控制器类中,如下所示:

if (([settings gameType] == SET_NUMBER_OF_MOVES) && (numMoves == [settings numMovesForLOD])) {
    [self showLoseScreen];
}

我收到错误(如 EXC_BAD_ACCESS)或条件总是失败。我做错了什么吗?

另外,我应该指出,我有这样的代码用于调用 gameType(位于 Settings 类中):

- (GamePlayType)gameType {
    return [dao gameType];
}

并且 DAO 实现这样的 gameType:

- (int)gameType {
    return (settingsContent != nil) ? [[settingsContent objectForKey:@"Game Type"] intValue] : 0;
}

我知道 DAO 返回一个 int 而不是 GamePlayType,但是 A)当我尝试使用“正确的”数据类型时,我描述的问题就出现了,B)我认为这并不重要,因为枚举只是一堆命名的整数,对吗?

任何帮助,非常感谢。我真的很想彻底理解这一点,但有些东西却让我无法理解……

干杯,

克里斯

I thought I had this sorted, but I am still missing something.

Very simply, I have a Settings class that hold a DAO (sitting on a plist). I want to have a couple of enums for the settings for convenience and readability, such as GamePlayType and DifficultyLevel. Right now I am defining them in the Settings.h file above the @interface line as such:

typedef enum {
 EASY,
 NORMAL,
 HARD
} DifficultyLevel;

and

typedef enum {
 SET_NUMBER_OF_MOVES,
 TO_COMPLETION
} GamePlayType;

If I access them from within the Settings class like:

- (int)gridSizeForLOD {
 switch ([self difficultyLevel]) {
  case EASY:
   return GRID_SIZE_EASY;
  case NORMAL:
   return GRID_SIZE_NORMAL;
  case HARD:
   return GRID_SIZE_HARD;
  default:
   return GRID_SIZE_NORMAL;
 }
}

everything is fine.

But, if I try to access them outside of the Settings class, let's say in my main view controller class, like this:

if (([settings gameType] == SET_NUMBER_OF_MOVES) && (numMoves == [settings numMovesForLOD])) {
    [self showLoseScreen];
}

I get errors (like EXC_BAD_ACCESS) or the condition always fails. Am I doing something incorrectly?

Also, I should point out that I have this code for the call to gameType (which lives in the Settings class):

- (GamePlayType)gameType {
    return [dao gameType];
}

and the DAO implements gameType like this:

- (int)gameType {
    return (settingsContent != nil) ? [[settingsContent objectForKey:@"Game Type"] intValue] : 0;
}

I know I have the DAO returning an int instead of a GamePlayType, but A) the problem I am describing arose there when I tried to use the "proper" data type, and B) I did not think it would matter since the enum is just a bunch of named ints, right?

Any help, greatly appreciated. I really want to understand this thoroughly, and something is eluding me...

Cheers,

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2024-09-12 17:53:36

如果您还没有它,您将需要在其他课程中进行如下导入

#import "Settings.h"

If you don't have it there already you'll need an import like below in your other class

#import "Settings.h"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文