正如标题所述,我需要此函数返回一个数组。但是,如果我将return [LocationDataModel]
放在URLSession闭包之外,则会得到null。或者,如果我将它放在闭包中,则会得到:Unexpected non-void return value in void function
。这是函数:
func getLocationJSON() -> [LocationDataModel] {
let uid = LoginController.GlobalLoginIDs.uid
let uuid = LoginController.GlobalLoginIDs.uuid
var locationsData = [LocationDataModel]()
var locationDataModel = LocationDataModel()
let urlString = "site deleted" + "/DataSource/Mobile/?Action=GetMyLocation&uid=" + uid + "&uuid=" + uuid
guard let url = URL(string: urlString) else {return locationsData}
URLSession.shared.dataTask(with: url) { (data, _ , error) in
if let err = error {
print("Error fetching data:", err)
}
guard let data = data else {return}
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
let requestResult = json["RequestResult"] as! [String: Any]
let locData = requestResult["DATA"] as! [String: Any]
let locations = locData["ROWS"] as! [[String: Any]]
for locationDetails in locations {
locationDataModel.id = locationDetails["Id"] as? String
locationDataModel.userRef = locationDetails["UserRef"] as? String
locationDataModel.userRefName = locationDetails["UserRefName"] as? String
locationDataModel.name = locationDetails["Name"] as? String
locationDataModel.desc = locationDetails["Desc"] as? String
locationDataModel.locationOwner = locationDetails["LocationOwner"] as? String
locationDataModel.locationOwnerName = locationDetails["LocationOwnerName"] as? String
locationDataModel.isHouseLocation = locationDetails["IsHouseLocation"] as? String
locationDataModel.closeOnPublicHolidays = locationDetails["CloseOnPublicHolidays"] as? String
locationDataModel.itemHolding = locationDetails["ItemHolding"] as? String
locationDataModel.totalItemHolding = locationDetails["TotalItemHolding"] as? String
locationDataModel.unitNo = locationDetails["UnitNo"] as? String
locationDataModel.streetNo = locationDetails["StreetNo"] as? String
locationDataModel.streetName = locationDetails["StreetName"] as? String
locationDataModel.city = locationDetails["City"] as? String
locationDataModel.stateProvince = locationDetails["StateProvince"] as? String
locationDataModel.country = locationDetails["Country"] as? String
locationDataModel.zipPostalCode = locationDetails["ZipPostalCode"] as? String
locationDataModel.lat = locationDetails["lat"] as? String
locationDataModel.long = locationDetails["lng"] as? String
locationDataModel.staticLocation = locationDetails["StaticLocation"] as? String
locationDataModel.status = locationDetails["Status"] as? String
locationDataModel.statusName = locationDetails["StatusName"] as? String
locationDataModel.operatingStatus = locationDetails["OperatingStatus"] as? String
locationDataModel.operatingStatusName = locationDetails["OperatingStatusName"] as? String
var locationsDataArray = [LocationDataModel]()
locationsDataArray.append(locationDataModel)
locationsData = locationsDataArray
return locationsData
}
}catch let err {
print("Error Serializing Location Data:" , err)
}
}.resume()
return locationsData
}
第一个return locationsData
引发错误。在URLSession被解雇之前,第二个被解雇了。我觉得我必须从封闭内部而不是外部检索数据,否则它将为零。请帮我。谢谢!