我试图在下一种情况下使用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
};
我有一个警告:
隐式转换失去整数精度:'unsigned int'为'BOOL'(又名'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没有帮助我。我做错了什么?
答案 0 :(得分:1)
问题是registry.animalTypes & FZAnimalTypeBear
这样的表达式不会产生BOOL
。它仍然是NSUInteger
。如果整数值对于BOOL
类型而言太大,则会收到警告。
您需要将结果更改为实际BOOL
。以下内容可行:
zoo.hasEagle = (registry.animalTypes & FZAnimalTypeEagle) != 0;
对所有支票进行此更改。
BOOL
要注意的另一件事是,最好只使用YES
和NO
作为可能的值。尽量不要将任意整数值分配给BOOL
类型。