Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中使用结构处理按位操作?_Swift - Fatal编程技术网

如何在Swift中使用结构处理按位操作?

如何在Swift中使用结构处理按位操作?,swift,Swift,在Obj-C中,我喜欢做以下事情: [MyUILib makePathForRectWithEdges:UIRectEdgeLeft|UIRectEdgeRight] 其中的参数是NSInteger,稍后我将其与UIRectEdge枚举值进行比较,以查看要在路径中包括哪些边: if(edges & UIRectEdgeTop) { // Draw Stuff } 在Swift中,苹果将UIDirectEdge定义为: struct UIRectEdge : OptionSetT

在Obj-C中,我喜欢做以下事情:

[MyUILib makePathForRectWithEdges:UIRectEdgeLeft|UIRectEdgeRight]
其中的参数是NSInteger,稍后我将其与UIRectEdge枚举值进行比较,以查看要在路径中包括哪些边:

if(edges & UIRectEdgeTop)
{
   // Draw Stuff
}
在Swift中,苹果将UIDirectEdge定义为:

struct UIRectEdge : OptionSetType {
    init(rawValue rawValue: UInt)
    static var None: UIRectEdge { get }
    static var Top: UIRectEdge { get }
    static var Left: UIRectEdge { get }
    static var Bottom: UIRectEdge { get }
    static var Right: UIRectEdge { get }
    static var All: UIRectEdge { get }
}

我仍然可以使用
uirectredge.Top.rawValue
来获取UInt,但是调用类似于:
makePathForRectWithEdge(uirectredge.Left.rawValue | uirectredge.Right.rawValue)这样的函数与我以前的做法相比,既难看又难读。是否有更好的模式用于使用标志,或者这正是Swift中必须采用的方式?

您向函数发送一个集合,而不是对原始值进行排序或重新排序:

MyUILib.makePathForRectWithEdges([.Left, .Right])
因此名称为
选项setype
。当需要检查集合中是否包含选项时:

if edges.contains(.Top) { ... }