I'm trying to call the API and when I pass the value to the result variable it gives me an error. I also get this error on the func mapError function below.
Any idea on why am I getting this error?
Thanks in advance
'Result[Data, FourSquareError]' cannot be constructed because it has no accessible initializers
'Result[Data, E]' cannot be constructed because it has no accessible initializers
The errors are on these lines:
let result = Result<Data, FourSquareError>(value: data, error:
translatedError)
return Result<Value, E>(value)
return Result<Value, E>(try transform (error))
Here's the full code:
typealias JSON = [String: Any]
enum FourSquareError: Error {
case couldNotCreateURL
case networkError(Error)
case serverError(errorType: String, errorDetail: String)
case couldNotParseData
}
func createURL(endpoint: String, parameters: [String: String]) -> URL? {
let baseURL = "https://api.foursquare.com/v2/"
// We convert the parameters dictionary in an array of URLQueryItems
var queryItems = parameters.map { pair -> URLQueryItem in
return URLQueryItem(name: pair.key, value: pair.value)
}
// Add default parameters to query
queryItems.append(URLQueryItem(name: "v", value: apiVersion))
queryItems.append(URLQueryItem(name: "client_id", value: clientId))
queryItems.append(URLQueryItem(name: "client_secret", value: clientSecret))
var components = URLComponents(string: baseURL + endpoint)
components?.queryItems = queryItems
return components?.url
}
func getVenues(latitude: Double, longitude: Double, completion: @escaping
(Result<[JSON], FourSquareError>) -> Void) {
let parameters = [
"ll": "\(latitude),\(longitude)",
"intent": "browse",
"radius": "250"
]
guard let url = createURL(endpoint: "venues/search", parameters:
parameters)
else {
completion(Result.failure(.couldNotCreateURL))
return
}
let task = URLSession.shared.dataTask(with: url) { data, response,
error in
let translatedError = error.map { FourSquareError.networkError($0) }
let result = Result<Data, FourSquareError>(value: data, error:
translatedError)
// Parsing Data to JSON
.flatMap { data in
do {
return Result.success(try parseData(data))
} catch {
return Result.failure(.unexpectedError(error))
}
}
// Check for server errors
.flatMap { (json: JSON) -> Result<JSON, FourSquareError> in
do {
return Result.success(try validateResponse(json: json))
} catch {
return Result.failure(.unexpectedError(error))
}
}
// Extract venues
.flatMap { (json: JSON) -> Result<[JSON], FourSquareError> in
do {
return Result.success(try extractVenues(json: json))
} catch {
return Result.failure(.unexpectedError(error))
}
}
completion(result)
}
task.resume()
}
extension Result {
public func mapError<E: Error>(_ transform: (ErrorType) throws -> E) rethrows -> Result<Value, E> {
switch self {
case .success(let value):
return Result<Value, E>(value)
case .failure(let error):
return Result<Value, E>(try transform (error))
}
}
}
Result is an enum so you can construct it. You can only instantiate a Result
instance through one of it's two cases: failure
or success
. For example:
let result: Result<Data, FourSquareError> = .success(Data())
or
let result: Result<Data, FourSquareError> = .failure(FourSquareError.whatever)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.