"No exact matches in call to initializer" using OpenKitten/BSON package

I am trying to use the OpenKitten/BSON Swift package on a new project, and I’m unable to access any values on a created Document while following the README instructions.

The compiler is unable to use the value.

let data: Document = [ "type": "createSession" ] let type = String(data["type"]) //         ^ No exact matches in call to initializer  print(type); 

If I add a breakpoint and observe the value using lldb, it does have a debugger output, so the data exists:

(lldb) vo data (BSON.Document) data = ▿ ["type": "createSession"]   ▿ storage : ByteBuffer (...) 

I’m still learning Swift, and it seems this library implements a lot of advanced concepts that I’m still not sure how to properly unwrap.

What am I doing wrong here?

Add Comment
1 Answer(s)

It seems the REAMDE was outdated and with inaccurate information.

One way to properly decode a Document involves using BSONDecoder().decode(type, from), such as this:

struct Message: Codable {   let type: String; }  let data: Document = [ "type": "createSession" ]  do {   let message = try BSONDecoder().decode(Message.self, from: Document(data: data))   print(message.type);   // This should work } catch {   print("error is \(error.localizedDescription)") } 
Answered on August 31, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.