Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with Arrays #70

Open
pronebird opened this issue Dec 29, 2016 · 1 comment
Open

Issues with Arrays #70

pronebird opened this issue Dec 29, 2016 · 1 comment

Comments

@pronebird
Copy link

pronebird commented Dec 29, 2016

I have a structure called OTKSession that conforms to JSONDecodable, however using in it context of Array within generics does not work and I get the following error:

Client.swift:128:19: In argument type '(Result<[OTKSession]>) -> Void', '[OTKSession]' does not conform to expected type 'JSONDecodable'

Example code:

func rest<ResponseType: JSONDecodable>(_ completionHandler: @escaping (Result<ResponseType>) -> Void) {
  let value = ... // comes asynchronously
  let jsonString = String(bytes: value.0, encoding: .utf8)!
  let responseObject = try! ResponseType(JSONString: jsonString)
  completionHandler(.ok(responseObject))
}

Do I have to handle arrays separately or it is possible to incorporate them within single function? It seems like there is no way to constraint generics to Array of JSONDecodable objects.

At the moment I've added overloaded method that wraps ResponseType in array. Two methods coexist just fine but it seems like a code duplication to me which probably can be solved in more elegant way.

func rest<ResponseType: JSONDecodable>(_ completionHandler: @escaping (Result<[ResponseType]>) -> Void) {
  let value = ... // comes asynchronously
  let jsonString = String(bytes: value.0, encoding: .utf8)!
  let responseObject = try! [ResponseType](JSONString: jsonString)
  completionHandler(.ok(responseObject))
}
@Nadohs
Copy link
Collaborator

Nadohs commented Jan 15, 2017

Hi thanks for asking, but unfortunately I do not have a good answer to this.. In fact we are handling similar cases like this within the JSONCodable library itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants