Swift枚举中的默认参数

nik*_*ers 6 enums default-arguments swift

我创建了一个SnapOperationQueue,它是一个操作队列,有一些我喜欢的扩展.其中之一是对操作组进行重新优先排序.添加了一个操作,其中包含以下四个优先级之一(SnapOperationQueuePriority):

case Highest
case High
case Normal
case Low
Run Code Online (Sandbox Code Playgroud)

在当前的实现中,我有它.Low和.Highest不会改变,但.High和.Normal会.

我想改变这一点,以便我可以在优先级上设置上限和下限.也就是说,我可以说我们现在做的这个操作(.低)可能会变得非常重要(.High),即当预先缓存图像时,屏幕上突然需要该图像.这些阈值也应该能够说某些操作,即使在重新优先级的组中,也不会重新优先级低于某些操作,即应保留登录操作(.Highest).最高

为此,我想修改我的枚举,如下所示:

case Highest(lowerThreshold: SnapOperationQueuePriority = .Highest)
case High(lowerThreshold: SnapOperationQueuePriority = .Low, higherThreshold: SnapOperationQueuePriority = .High)
case Normal(lowerThreshold: SnapOperationQueuePriority = .Low, higherThreshold: SnapOperationQueuePriority = .High)
case Low(higherThreshold: SnapOperationQueuePriority = .Low)
Run Code Online (Sandbox Code Playgroud)

但是,我得到"在一个类型中不允许默认参数".我想在此处拥有默认参数的原因是,此更改不会破坏或更改已使用现有实现的任何代码的行为.

您是否可以建议我可以获得新优先级但不更改依赖于当前API的代码的API?

ske*_*ech 6

仅供参考:Swift 5.1 之后现在允许使用默认参数值。这个答案仅适用于之前的 Swift 版本。


这是一个棘手的问题,因为你不能有默认值,不能有存储的属性,也不能在枚举中重复案例名称。我能想到的最好的事情就是创建一个 init() 和一些私有的案例。

enum SnapOperationQueuePriority {
    case Highest, High, Low, Default
}

enum SnapOperationQueue {
    case Highest, High, Normal, Low

    case _Highest(lowerThreshold: SnapOperationQueuePriority)
    case _High(lowerThreshold: SnapOperationQueuePriority, higherThreshold: SnapOperationQueuePriority)
    case _Normal(lowerThreshold: SnapOperationQueuePriority, higherThreshold: SnapOperationQueuePriority)
    case _Low(higherThreshold: SnapOperationQueuePriority)

    init(queue:SnapOperationQueue, lowerThreshold:SnapOperationQueuePriority = .Default, higherThreshold:SnapOperationQueuePriority = .Default) {
        switch queue {
        case .Highest:
            self = ._Highest(lowerThreshold: lowerThreshold == .Default ? .Highest : lowerThreshold)
        case .High:
            self = ._High(lowerThreshold: lowerThreshold == .Default ? .Low : lowerThreshold, higherThreshold: higherThreshold == .Default ? .High : higherThreshold)
        case .Normal:
            self = ._Normal(lowerThreshold: lowerThreshold == .Default ? .Low : lowerThreshold, higherThreshold: higherThreshold == .Default ? .High : higherThreshold)
        case Low:
            self = ._Low(higherThreshold: higherThreshold == .Default ? .Low : higherThreshold)
        default:
            self = queue

        }
    }
}

SnapOperationQueue.Normal
SnapOperationQueue(queue: .Normal)
SnapOperationQueue(queue: .High, lowerThreshold: .High, higherThreshold: .Highest)
Run Code Online (Sandbox Code Playgroud)

这使旧的实现保持有效并捕获使用 init 生成的新实现。此外,您可以将这样的方法添加到枚举中:

func queue() -> SnapOperationQueue {
    switch self {
    case .Highest:
        return SnapOperationQueue(queue: .Highest)
    case .High:
        return SnapOperationQueue(queue: .High)
    case .Normal:
        return SnapOperationQueue(queue: .Normal)
    case Low:
        return SnapOperationQueue(queue: .Low)
    default:
        return self

    }

}
Run Code Online (Sandbox Code Playgroud)

这样您就可以将旧类型的枚举案例转换为新类型,例如SnapOperationQueue.Normal.queue()


Gob*_*obe 5

现在从 Swift 5.1 (Xcode 11) 开始可以实现这一点

请参阅功能提案。