Swift模式与枚举和可选元组相关联的值匹配

编程入门 行业动态 更新时间:2024-10-24 18:17:32
本文介绍了Swift模式与枚举和可选元组相关联的值匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Alamofire,我使用枚举来描述我在自述文件中建议使用的API。

端点表示如下: p>

public enum API { case GetStops(stopCode:String?) case GetPhysicalStops case GetLinesColors case GetNextDepartures(stopCode:String,departureCode:String?,linesCode:String?,destinationCode:String?)}

可选参数是互斥的:

public var URLRequest:NSMutableURLRequest { let result((path:String,参数:[String:AnyObject]?)= { switch self { case .GetStops(let stopCode)where stopCode!= nil: return(GetStops.json,[stopCode:stopCode!]) case .GetStops(_): return(GetStops.json,nil) case。 GetPhysicalStops:返回(GetPhysicalStops.json,nil) case .GetLinesColors: return(GetLinesColors,nil) case .GetNextDepartures(let stopCode,let departCode,_,_)where departureCode! = nil: return(GetNextDepartures,[stopCode:stopCode,departureCode:departureCode!]) case .GetNextDepartures(let stopCode,_,let linesCode,_)where linesCode!= nil: return(GetNextDepartures,[stopCode:stopCode,linesCode:linesCode!]) case .GetNextDepartures(let stopCode,_,_,let destinationsCode)where destinationsCode!= nil : return(GetNextDepartures,[stopCode:stopCode,destinationsCode:destinationsCode!]) case .GetNextDepartures(let stopCode,_,_,_): return GetNextDepartures,[stopCode:stopCode])} }()

有没有办法解开一个在这个语句中,可选的包含(如 if let ),并避免在此语句中显式展开:

case .GetStops(let stopCode)where stopCode!= nil: return(GetStops.json,[stopCode:stopCode!])

解决方案

您可以使用 .Some(x) pattern( .some(x)):

case .GetStops(let .Some(stopCode)): return(GetStops.json,[stopCode:stopCode])

从Swift 2(Xcode 7)开始,这可以简写为 x? pattern:

case .GetStops(let stopCode?): return(GetStops.json,[stopCode:stopCode])

相关值被测试为非零和解包(类似于可选绑定)。 / p>

I'm currently using Alamofire and I use an enum to describe the API I used as advised in the readme.

The endpoints are represented as follows:

public enum API { case GetStops(stopCode:String?) case GetPhysicalStops case GetLinesColors case GetNextDepartures(stopCode:String, departureCode:String?, linesCode:String?, destinationsCode:String?) }

The optional parameters are mutually exclusive:

public var URLRequest: NSMutableURLRequest { let result:(path:String, parameters:[String:AnyObject]?) = { switch self { case .GetStops(let stopCode) where stopCode != nil : return ("GetStops.json", ["stopCode" : stopCode!]) case .GetStops(_): return ("GetStops.json", nil) case .GetPhysicalStops: return ("GetPhysicalStops.json", nil) case .GetLinesColors: return ("GetLinesColors",nil) case .GetNextDepartures(let stopCode, let departureCode, _, _) where departureCode != nil: return ("GetNextDepartures", ["stopCode" : stopCode, "departureCode": departureCode!]) case .GetNextDepartures(let stopCode, _, let linesCode, _) where linesCode != nil: return ("GetNextDepartures", ["stopCode" : stopCode, "linesCode": linesCode!]) case .GetNextDepartures(let stopCode, _, _, let destinationsCode) where destinationsCode != nil: return ("GetNextDepartures", ["stopCode" : stopCode, "destinationsCode": destinationsCode!]) case .GetNextDepartures(let stopCode,_,_,_): return ("GetNextDepartures",["stopCode":stopCode]) } }()

Is there a way to unwrap automatically the optional contained (like if let) within the tuple and avoiding to explicity unwrap like in this statement :

case .GetStops(let stopCode) where stopCode != nil : return ("GetStops.json", ["stopCode" : stopCode!])

解决方案

You can use the .Some(x) pattern (.some(x) in Swift 3):

case .GetStops(let .Some(stopCode)): return ("GetStops.json", ["stopCode" : stopCode])

As of Swift 2 (Xcode 7), this can be shorter written as x? pattern:

case .GetStops(let stopCode?): return ("GetStops.json", ["stopCode" : stopCode])

The associated value is tested to be non-nil and unwrapped (similar as in an optional binding).

更多推荐

Swift模式与枚举和可选元组相关联的值匹配

本文发布于:2023-10-19 14:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相关联   可选   模式   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!