I am beginner of swift, I had a thread when I parse json data, the object result is nil, can anyone help me to figure out what the problem is. if the func return void, I can get the correct data, but I can not return it as a array. thanks
struct catalogobject: Hashable, Decodable, Encodable{
let objects: [CatalogObject]?
}
func getCatalog() -> [CatalogObject] {
var object : [CatalogObject]!
let url = URL(string: "https://connect.squareupsandbox.com/v2/catalog/list")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.addValue("\(content_type)", forHTTPHeaderField: "Content-Type")
request.addValue("\(square_version)", forHTTPHeaderField: "Square-Version")
URLSession.shared.dataTask(with: request) { data, res, error in
let data = data!
do {
let catalogdata = try JSONDecoder().decode(catalogobject.self, from: data)
object = catalogdata.objects
print("object \(object)")
}catch{
print(error)
}
}.resume()
return object
}