Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean in Swift: 'case .success(let dict):'

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?

like image 368
konrad Avatar asked Mar 31 '17 12:03

konrad


1 Answers

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.

like image 95
Losiowaty Avatar answered Sep 22 '22 20:09

Losiowaty