gpt4 book ai didi

swift - 如何在 Swift 中使用结构处理按位运算?

转载 作者:可可西里 更新时间:2023-11-01 01:35:45 26 4
gpt4 key购买 nike

<分区>

在 Obj-C 中,我喜欢做这样的事情:

[MyUILib makePathForRectWithEdges:UIRectEdgeLeft|UIRectEdgeRight]

其中的参数是一个 NSInteger,稍后我将其与 UIRectEdge 枚举值进行比较以查看我想在路径中包含哪些边:

if(edges & UIRectEdgeTop)
{
// Draw Stuff
}

在 Swift 中,Apple 将 UIRectEdge 定义为:

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 }
}

我仍然可以使用 UIRectEdge.Top.rawValue 来获取 UInt,但调用的函数如下:makePathForRectWithEdges(UIRectEdge.Left.rawValue|UIRectEdge.Right.rawValue) 与我以前的做法相比,它丑陋且难以阅读。是否有更好的模式来使用标志,或者这就是它在 Swift 中必须采用的方式?

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