如何在Swift中为SpriteKit定义类别位掩码枚举? [英] How to define category bit mask enumeration for SpriteKit in Swift?

查看:196
本文介绍了如何在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 enumerations 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆