调用多个变量的简单而整洁的方式

发布于 2024-11-08 06:02:22 字数 615 浏览 6 评论 0原文

我想做类似的事情

int ItemNames;
typedef enum ItemNames {apple, club, vial} ItemNames;    
+(BOOL)GetInventoryItems{return ItemNames;}
apple=1; //Compiler Error.

,问题是,我无法将枚举中的变量设置为新值。编译器告诉我,我已经在枚举中“重新声明”了一个整数。此外,它不会正确返回值。 因此,我必须对每个项目使用 if 语句来检查它是否存在,如下所示。

+ (void)GetInventoryItems
{
    if (apple <= 1){NSLog(@"Player has apple");}
    if (club <= 1){ NSLog(@"Player has club");}
    if (vial <= 1){NSLog(@"Player has vial");}
    if (apple == 0 && club == 0 && vial == 0){NSLog(@"Player's Inventory is Empty.");}
}

有解决办法吗?

I'm looking to do something like

int ItemNames;
typedef enum ItemNames {apple, club, vial} ItemNames;    
+(BOOL)GetInventoryItems{return ItemNames;}
apple=1; //Compiler Error.

The issue is, is that I cannot set a variable in an enum to a new value. The compiler tells me that I have "redeclared" an integer in the enum. Also, it won't correctly return the values.
So instead I have to use an if statement for each item to check if it exists like so.

+ (void)GetInventoryItems
{
    if (apple <= 1){NSLog(@"Player has apple");}
    if (club <= 1){ NSLog(@"Player has club");}
    if (vial <= 1){NSLog(@"Player has vial");}
    if (apple == 0 && club == 0 && vial == 0){NSLog(@"Player's Inventory is Empty.");}
}

Is there a work around?

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

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

发布评论

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

评论(3

戒ㄋ 2024-11-15 06:02:22

您试图使用错误的数据结构。枚举只是可能值的列表,是一种数据类型,而不是变量。

typedef struct {
  int apple : 1;
  int club : 1;
  int vial : 1;
}
inventory_type;

inventory_type room;

room.apple = 1;

if (room.apple) NSLog (@"There's an apple");
if (room.club) NSLg (@"There's a club!");

typedef 每个元素后面的冒号和数字告诉编译器要使用多少位,因此在这种情况下,可以使用单个位(即二进制值)。

You're trying to use the wrong data structure. An enum is just a list of possible values, a data type and not a variable.

typedef struct {
  int apple : 1;
  int club : 1;
  int vial : 1;
}
inventory_type;

inventory_type room;

room.apple = 1;

if (room.apple) NSLog (@"There's an apple");
if (room.club) NSLg (@"There's a club!");

The colon and number after each element of the typedef tells the compiler how many bits to use, so in this case a single bit (i.e., a binary value) is available.

清风挽心 2024-11-15 06:02:22

枚举值是常量,因此无法修改。 Objective-c 是一种基于 C 的语言,因此 ItemNames 不是一个对象,而是一种类型。

The enum values are constants, so they can't be modified. Objective-c is a c-based language, so ItemNames isn't an object, it is a type.

纸伞微斜 2024-11-15 06:02:22

我发现很难理解你的问题。您确定知道 enum 在 C 中如何工作吗?这只是一种方便地声明数字常量的方法。例如:

enum { Foo, Bar, Baz };

是这样的:

static const NSUInteger Foo = 0;
static const NSUInteger Bar = 1;
static const NSUInteger Baz = 2;

如果您想将多个库存项目打包到一个值中,您可以使用一个位字符串:

enum {
    Apple  = 1 << 1,
    Banana = 1 << 2,
    Orange = 1 << 3
};

NSUInteger inventory = 0;

BOOL hasApple  = (inventory & Apple);
BOOL hasBanana = (inventory & Banana);

inventory = inventory | Apple; // adds an Apple into the inventory

希望这会有所帮助。

I find it hard to wrap my head around your question. Are you sure you know how enum works in C? It’s just a way to conveniently declare numeric constants. For example:

enum { Foo, Bar, Baz };

Is something like:

static const NSUInteger Foo = 0;
static const NSUInteger Bar = 1;
static const NSUInteger Baz = 2;

If you want to pack several inventory items into a single value, you might use a bit string:

enum {
    Apple  = 1 << 1,
    Banana = 1 << 2,
    Orange = 1 << 3
};

NSUInteger inventory = 0;

BOOL hasApple  = (inventory & Apple);
BOOL hasBanana = (inventory & Banana);

inventory = inventory | Apple; // adds an Apple into the inventory

Hope this helps.

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