I'm trying to make an app for iPhone, which should consume webservice written in nodejs + MongoDB. The application is made in Swift, but now I have a problem, I have not managed correctly parse the data.
Currently I have a code like this:
var endpoint = NSURL(string: self.url + "?latitud=" + self.latitude + "&longitud=" + self.longitude)
var data = NSData(contentsOfURL: endpoint!)
var error: NSError? = nil
if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
for place in json {
var name = place["obj"]["name"]
var coords = place["obj"]["coords"]
var annotation = MKPointAnnotation()
annotation.title = name as? String
annotation.coordinate = coords as? CLLocationCoordinate2D
map.addAnnotation(annotation)
}
}
Unfortunately it does not work :(
The response from the webservice is similar to this:
[
{
"dis": 1.22,
"obj": {
"name": "Some name",
"coords": [
-97.1228,
17.4049
],
"phones": [
"555 555 55555",
"444 444 44444"
],
"address": {
"street": "Some Street",
"zip": "00000"
}
}
},
{
"dis": 2.03,
"obj": {
"name": "Othe name",
"coords": [
-97.0910
17.7099
],
"phones": [
"777 777 7777"
],
"address": {
"street": "Other street",
"zip": "11111"
}
}
}
]
That's what I'm doing wrong? Is there a more elegant (and especially efficient) way to make requests to the API RESTful?