Search code examples
swiftalamofire

How to express Result<Value, Error: Errortype> in a method signature


I've been trying to make an Alamofire upgrade from 2.0 to 3.0. One of the methods contains this signature:

func standardResponse(request: NSURLRequest?, response: NSHTTPURLResponse?, result: Result<AnyObject>, success: (object: AnyObject?) -> Void, failure: (error: ServerError) -> Void)

There's an error pointing at Result<AnyObject>, stating that generic type 'Result' specialized with too few type parameters (got 1, but expected 2)

Alright, so I put in 2. According to the Alamofire 3.0 migration guide, Result has changed to accommodate an extra Error: ErrorType parameter. I tried this next:

Result<AnyObject, Error>

This time the error was that Error does not conform to protocol ErrorType.

So maybe this?

Result<AnyObject, Error: ErrorType>

No cigar. Please help me understand.


Solution

  • It looks like the second parameter has to be an object conforming to ErrorType.

    So you could for example create your own error type with an enum like this:

    enum MyErrorType: ErrorType { 
        case SomeError 
        case SomeOtherError 
    }
    

    Then use it the way the compiler asks:

    Result<AnyObject, MyErrorType>