gpt4 book ai didi

ios - 具有超过 8 个值的 NS_OPTIONS

转载 作者:行者123 更新时间:2023-11-29 00:24:47 25 4
gpt4 key购买 nike

在下一个案例中我尝试使用 NS_OPTIONS:

typedef NS_OPTIONS(NSUInteger, FZAnimalType) {
FZAnimalTypeNone = 0,
FZAnimalTypeBear = 1 << 0,
FZAnimalTypeFox = 1 << 1,
FZAnimalTypeWolf = 1 << 2,
FZAnimalTypeTiger = 1 << 3,
FZAnimalTypeLion = 1 << 4,
FZAnimalTypeRabbit = 1 << 5,
FZAnimalTypeGoat = 1 << 6,
FZAnimalTypeParrot = 1 << 7,
FZAnimalTypeDog = 1 << 8,
FZAnimalTypeCat = 1 << 9,
FZAnimalTypeSheep = 1 << 10,
FZAnimalTypeEagle = 1 << 11
};

我有一个警告:

Implicit conversion loses integer precision: 'unsigned int' to 'BOOL' (aka 'signed char')

在以下代码的最后 4 行:

zoo.hasBear = registry.animalTypes & FZAnimalTypeBear;
zoo.hasFox = registry.animalTypes & FZAnimalTypeFox;
zoo.hasWolf = registry.animalTypes & FZAnimalTypeWolf;
zoo.hasTiger = registry.animalTypes & FZAnimalTypeTiger;
zoo.hasLion = registry.animalTypes & FZAnimalTypeLion;
zoo.hasRabbit = registry.animalTypes & FZAnimalTypeRabbit;
zoo.hasGoat = registry.animalTypes & FZAnimalTypeGoat;
zoo.hasParrot = registry.animalTypes & FZAnimalTypeParrot;
zoo.hasDog = registry.animalTypes & FZAnimalTypeDog;
zoo.hasCat = registry.animalTypes & FZAnimalTypeCat;
zoo.hasSheep = registry.animalTypes & FZAnimalTypeSheep;
zoo.hasEagle = registry.animalTypes & FZAnimalTypeEagle;

和属性 hasDog/hasCat/hasSheep/hasEagle - 总是 NO。这篇文章中的解决方案:64-bit NS_OPTIONS bitmask没有帮助我。我做错了什么?

最佳答案

问题是像 registry.animalTypes & FZAnimalTypeBear 这样的表达式不会产生 BOOL。它仍然是一个 NSUInteger。如果整数值对于 BOOL 类型来说太大,您会收到警告。

您需要将结果更改为实际的 BOOL。像下面这样的东西会起作用:

zoo.hasEagle = (registry.animalTypes & FZAnimalTypeEagle) != 0;

对您的所有支票进行此更改。

关于 BOOL 的另一件事是最好只使用 YESNO 作为可能的值。尽量不要将任意整数值分配给 BOOL 类型。

关于ios - 具有超过 8 个值的 NS_OPTIONS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com