How to fix this ambiguous type error in Swift?

So, I created a new Xcode project and wrote this Podfile:

use_frameworks!

target 'Repro' do
  pod 'Alamofire'
  pod 'Result'
end

Then I started pod install, opened the workspace and created a new file with the following contents:

import Alamofire
import Result

private func something(request: Request) -> Result<Bool, NSError> {
    fatalError()
}

And I tried to build this, but Xcode created an error 'Result' is ambiguous for type lookup in this context. So I tried to make an obvious fix:

import Alamofire
import Result

private func something(request: Request) -> Result.Result<Bool, NSError> {
    fatalError()
}

But this gave me an error Reference to generic type 'Result' requires arguments in <...>, as if Swift parsed the module name as a type name.

What is a non-obvious fix?

+4
source share
2 answers

If you directly import the Result module type Result, it will override the Alamofire type Result. You can reach Alamofire using your qualified name:

import Alamofire
import enum Result.Result

let a: Alamofire.Result<T, ErrorType> // Alamofire Result
let r: Result<T, ErrorType> // Result module Result

, , . , , .

+7

, Result cocoapod 2.1 - -

private func something(request: Request) throws -> Result {
  // TODO: implement this 
  throw RequestError()
}

.


(Bool, NSError) . .

-1

Source: https://habr.com/ru/post/1626449/


All Articles