In Swift PromiseKit library there's Alamofire example using a bit strange syntax:
func login(completionHandler: (NSDictionary?, ErrorProtocol?) -> Void {
Alamofire.request(.GET, url, parameters: ["foo": "bar"])
.validate()
.responseJSON { response in
switch response.result {
case .success(let dict):
completionHandler(dict, nil)
case .failure(let error):
completionHandler(nil, error)
}
}
}
response
is an Alamofire enum describing two cases with associated values:
public enum Result<Value> {
case success(Value)
case failure(Error)
(...)
What I don't get is what does let
mean in each case:
line and where does the dict (or error) come from? Is this syntactic sugar for something more verbose but less confusing?
In Swift, enums can have associated values (docs). This means, that you can associate an object with cases. The part (let dict)
simply means - take the associated value, and put in in a let
constant named dict
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With