如何在 Swift 中为 SpriteKit 定义类别位掩码枚举? [英] How to define category bit mask enumeration for SpriteKit in Swift?
本文介绍了如何在 Swift 中为 SpriteKit 定义类别位掩码枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了在 Objective-C 中定义一个类别位掩码枚举,我曾经键入:
To define a category bit mask enum in Objective-C I used to type:
typedef NS_OPTIONS(NSUInteger, CollisionCategory)
{
CollisionCategoryPlayerSpaceship = 0,
CollisionCategoryEnemySpaceship = 1 << 0,
CollisionCategoryChickenSpaceship = 1 << 1,
};
如何使用 Swift
达到同样的效果?我尝试了枚举,但无法正常工作.这是我到目前为止所尝试的.
How can I achieve the same using Swift
? I experimented with enums but can't get it working. Here is what I tried so far.
推荐答案
你可以做的是使用二进制文字:0b1
, 0b10
, 0b100
等
What you could do is use the binary literals: 0b1
, 0b10
, 0b100
, etc.
但是,在 Swift 中,您不能对枚举进行按位或运算,因此在枚举中使用位掩码确实没有意义.查看这个问题来替代NS_OPTION.
However, in Swift you cannot bitwise-OR enums, so there is really no point in using bitmasks in enums. Check out this question for a replacement for NS_OPTION.
这篇关于如何在 Swift 中为 SpriteKit 定义类别位掩码枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文