我想做类似的事情
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.");}
}
有解决办法吗?