1

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.

Community
  • 1
  • 1
KML
  • 2,302
  • 5
  • 26
  • 47
  • `NSJSONSerialization.JSONObjectWithData` only works with *JSON data* (coming from a server, for example). Your `allConferences` object is an array of `METDocument` objects, not data, so it's normal it doesn't work. :) – Eric Aya Nov 03 '15 at 15:54
  • Ok, so is there a method for me to easily access the data? – KML Nov 03 '15 at 15:56

1 Answers1

1

NSJSONSerialization.JSONObjectWithData only works with JSON data (coming from a server, for example). Your allConferences object is an array of METDocument objects, not data, so it's normal it doesn't work.

Reading through the documentation, it looks like you can get the dictionary from a METDocument by using its fields property.

So to extract dictionaries from your objects, you could do this:

var dicts: [[String:AnyObject]] = []
for document in conferences.allDocuments {
    if let fields = document.fields as? [String:AnyObject] {
        dicts.append(fields)
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • wowI I have been working on this problem on and off since Oct 27. I owe you a huge amount of gratitude. Thank you, this solves mye problem. Now I just need to sort the Array it according to the createdAt Dictionary fields: `let createdAt = dicts[0]["createdAt"] as! NSDate` – KML Nov 03 '15 at 16:15
  • @karimi you have the data in NSDictionary. making another dictionary has no sense for me. dictionary is by the nature unordered collection. if you like to sort the dictionary by the values createdAt, i recommend you to find some tutorial about collections. ordered dictionary is easy to implement and there is a lot of example 'ready to use' on the web. – user3441734 Nov 03 '15 at 16:23
  • @user3441734 OP is going to sort an *array* of dictionaries using each dictionary's "createdAt" property, it's not the same. :) Karlml just typoed "dictionary" instead of "dictionaries" in his comment. – Eric Aya Nov 03 '15 at 16:25
  • @Eric D. let allConferences = conferences.allDocuments; print(allConferences); let oneConferencesField: NSDictionary = conferences.allDocuments[0].valueForKey("fields") as! NSDictionary; now i see it :-) – user3441734 Nov 03 '15 at 16:34