加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift枚举中的默认参数

发布时间:2020-12-14 04:40:09 所属栏目:百科 来源:网络整理
导读:我制作 SnapOperationQueue这是一个操作队列,有一些我喜欢的扩展名.其中之一是对操作组进行重新优先排序.添加了一个操作,其中包含以下四个优先级之一(SnapOperationQueuePriority): case Highestcase Highcase Normalcase Low 在当前的实现中,我有它.Low和.
我制作 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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读