Search code examples
iosswiftswift2alamofire

Alamofire: ERROR: Cannot assign value of type 'Result<_,_>' to type 'Result'


I am using Alamofire for sending request and handling response. I created a simple class which uses Alamofire.Result type:

class MyHandler {
    private var _result: Alamofire.Result

    init(result: Alamofire.Result) {
        //ERROR: Cannot assign value of type 'Result<_,_>' to type 'Result'
        self._result = result
    }
}   

}

I am getting a weird error as shown above in my code. Here is the source code of Alamofire.Result . It is a Enum enum Result<Value, Error: ErrorType>.

I am using the same Alamofire.Result type in my class for self._result & the result passed in initialier.

Why am I getting this error? it looks like compiler doesn't think they are the same type... my xcode version is 7.3.1. Is this a xcode bug?


Solution

  • AlamoFire.Result is a generic type with two placeholders

    public enum Result<Value, Error: ErrorType> { ... }
    

    You can declare your class for concrete types for the placeholders, for example

    class MyHandler {
        private var _result: AlamoFire.Result<Int, NSError>
    
        init(result: AlamoFire.Result<Int, NSError>) {
            self._result = result
        }
    }
    

    But it is more likely that you want to declare a generic class:

    class MyHandler<Value, Error: ErrorType> {
        private var _result: AlamoFire.Result<Value, Error>
    
        init(result: AlamoFire.Result<Value, Error>) {
            self._result = result
        }
    }