Leem.fin Leem.fin - 6 months ago 9
Swift Question

weird error, I am using the same type but compiler says no

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 get weird error I show 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 I get 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?

Answer

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
    }
}