问题描述
我正在使用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)$在Swift 3中,c $ c> 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模式与枚举和可选元组相关联的值匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!