Weird array results

I want to return the array [Place]. But instead of parsing my results title, subtitle and coordinate i returns weird stuf like "0x6080004b3aa0".

I think the problem is this line: let place = Place(title:title,subtitle:subtitle!,coordinate: CLLocationCoordinate2DMake(latitude, longitude))






import MapKit

@objc class Place: NSObject {



var title: String?

/

/

/

var coordinate: CLLocationCoordinate2D

var subtitle: String?

/


init(title:String,subtitle:String, coordinate:CLLocationCoordinate2D){


self.title = title

self.coordinate = coordinate

self.subtitle = subtitle

/

/

/

/

/

}


static func getPlaces() -> [Place] {

guard let url = NSURL(string: "https:/


let request = NSMutableURLRequest(url: url as URL!)

var places = [Place]()

let task = URLSession.shared.dataTask(with: request as URLRequest) {data,response,error in

guard error == nil && data != nil else {

print ("Error:",error)

return

}

let httpStatus = response as? HTTPURLResponse

if httpStatus?.statusCode == 200

{ if data?.count != 0

{

let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary

let contacts = responseString["Sheet1"] as? [AnyObject]

for contact in contacts!{

var places = [Place]()

let title = contact["name"] as! String

let subtitle = contact["description"] as? String

let latitude = contact["latitude"] as? Double ?? 0, longitude = contact["longitude"] as? Double ?? 0

let place = Place(title:title,subtitle:subtitle!,coordinate: CLLocationCoordinate2DMake(latitude, longitude))

print(latitude)

print(place)

}

}

else {

print("No data got from url")

}

} else {

print("error httpsatus code is:", httpStatus!.statusCode)

}

}

task.resume()


return places as [Place]

}


}

extension Place: MKAnnotation { }

Well, perhaps there's some code missing, because your code doesn't make any sense. You create a "places" array but put nothing in it. Then, in the "for contact in" loop in the completion handler you create another "places" array (unrelated to the one you've already created), and again put nothing in it. You create a "place" object, then throw it away because you don't put it anywhere that persists longer than the loop iteration.


Aside from that, your 'getPlaces' function can't return an array of Place objects obtained via HTTP (and decoded from JSON) because the HTTP request is asynchronous, and won't complete before the function returns.


You'll have to rethink your approach.

Weird array results
 
 
Q