
case Highestcase Highcase normalcase 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.normalSnapOperationQueue(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()
总结以上是内存溢出为你收集整理的Swift枚举中的默认参数全部内容,希望文章能够帮你解决Swift枚举中的默认参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)