I receive several(0-10) objects from a Meteor server in an array. They might look like this in the console:
let allConferences = conferences.allDocuments
print(allConferences)
Console output:
[<METDocument key: <collection: conferences, ID: Hr3bw6pySG8G3TKzh>, fields: {
createdAt = "2015-11-03 13:43:05 +0000";
type = doctor;
user = KTsCySacEAiz3eDnf;
userdata = {
birthdate = "Male";
gender = "<null>";
};
}>, <METDocument key: <collection: conferences, ID: RmfQm96Kcj5JTfDQM>, fields: {
createdAt = "2015-11-03 13:40:12 +0000";
type = doctor;
user = KTsCySacEAiz3eDnf;
userdata = {
birthdate = "<null>";
gender = "<null>";
};
}>]
I need to get this data in a format that I can use in Swift 2.1 with ease. For example I need to sort the objects according to the createdAt field and then use some of the other fields in labels in tableView.
I have tried NSJSONSerialization as per this answer
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(allConferences, options: []) as? NSDictionary {
print(jsonResult)
}
} catch {
print(error)
}
But that give me the error Cannot convert value of type '[AnyObject]' to expected argument type 'NSData'
I can access the fields area directly with this method:
let oneConferencesField: NSDictionary = conferences.allDocuments[0].valueForKey("fields") as! NSDictionary
If anyone know how to parse this data as per the classic method with NSJSONSerialization that would be great - thank you.