微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift枚举中的默认参数

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

case Highest
case High
case normal
case Low

在当前的实现中,我有它.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)

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

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

解决方法

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

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

enum SnapOperationQueue {
    case Highest,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)

这使旧的实现保持有效并捕获使用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

    }

}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐