You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi guys, I really want to thank you for you awesome work, I think Marshal is the fastest and lightweight library to deal with JSON structures.
I'm facing a problem parsing a simple data structures:
import Foundation
import Marshal
class Student: Unmarshaling {
var id: String
var firstName: String
var lastName: String
var courses: [String:Int]
required init(object: MarshaledObject) throws {
self.firstName = try object.value(for: "first_name")
self.lastName = try object.value(for: "last_name")
self.courses = try object.value(for: "courses")
}
}
let json: MarshalDictionary = try! JSONSerialization.jsonObject(with: dataFromString, options: []) as! MarshalDictionary
let students: [String: Student] = try! json.value(for: "students")
I don't exactly how to assign key to model id and additionally I'm getting and error while extracting courses dictionary: fatal error: try!' expression unexpectedly raised an error: Type mismatch. Expected type Dictionary<String, Int> for key: courses. Got '__NSArray0
Do you have any suggestion? Thank you so much
The text was updated successfully, but these errors were encountered:
I'm not really sure where Marshal thinks you're getting an array from given your example. But that's what it seems to think.
As to the first part of the question, that's tricky. Marshal isn't designed around this use case. My best suggestion would be to not use any of Marshal's built in helpers for converting collections, and instead do your own for map over the key value pairs of your dictionary. Then, inside the loop, init a a new student object using the dictionary's value, and then set the id to the key.
Hi guys, I really want to thank you for you awesome work, I think Marshal is the fastest and lightweight library to deal with JSON structures.
I'm facing a problem parsing a simple data structures:
Here my model class:
I don't exactly how to assign key to model id and additionally I'm getting and error while extracting courses dictionary: fatal error:
try!' expression unexpectedly raised an error: Type mismatch. Expected type Dictionary<String, Int> for key: courses. Got '__NSArray0
Do you have any suggestion? Thank you so much
The text was updated successfully, but these errors were encountered: