Generic type 'Result' specialized with too many type parameters (got 2, but expected 1)

import Foundation

import Alamofire


enum DataError: Error {

case duplicateFailure

case nonExistent

case incomplete

}


class CharacterService {

func getCharacters(completion: @escaping (Result<Array<Character>, DataError>) -> ()) {

let urlString = "https://rickandmortyapi.com/api/character"

guard let url = URL(string: urlString) else { return }

URLSession.shared.dataTask(with: url) { (data, response, error) in

DispatchQueue.main.async {

if let error = error {

completion(.failure(error))

return

}

do {

let decoder = JSONDecoder()

decoder.keyDecodingStrategy = .convertFromSnakeCase

decoder.dateDecodingStrategy = .formatted(DateFormatter.iso8601Full)

let characterResult = try decoder.decode(CharacterResult.self, from: data ?? Data())

completion(.success(characterResult.characters ?? []))

} catch let jsonError {

completion(.failure(jsonError))

}

}

}.resume()

}

}

Replies

Where do you get the error ?


Note: please format code properly, using the formatter (<>) tool:


import Foundation
import Alamofire

enum DataError: Error {
    case duplicateFailure
    case nonExistent
    case incomplete
}

class CharacterService {
  
    func getCharacters(completion: @escaping (Result<array, DataError>) -> ()) {
      
        let urlString = "https://rickandmortyapi.com/api/character"
        guard let url = URL(string: urlString) else { return }
      
        URLSession.shared.dataTask(with: url) { (data, response, error) in
          
            DispatchQueue.main.async {
              
                if let error = error {
                    completion(.failure(error))
                    return
                }
              
                do {
                    let decoder =  JSONDecoder()
                    decoder.keyDecodingStrategy = .convertFromSnakeCase
                    decoder.dateDecodingStrategy = .formatted(DateFormatter.iso8601Full)
                    let characterResult = try decoder.decode(CharacterResult.self, from: data ?? Data())
                  
                    completion(.success(characterResult.characters ?? []))
                  
                } catch let jsonError {
                    completion(.failure(jsonError))
                }
            }
          
            }.resume()
    }
}



So now the question is: which line ?

Seems you have another `Result` in your project.


- Why do you have `import Alamofire in your code. I cannot find any parts using it in your code shown.

- If you are using some third party libraries, check all of them are made for the Xcode version you are using. Old versions may cause some errors.


Your code has some other faults including another issue on `Result`, seems you have just copied a code on the web without modifying it for your purpose.


You should better explaing what you really want to do.