无效函数中的意外非空值(Swift 3.0)

时间:2017-03-29 01:45:53

标签: swift xcode asynchronous

我正在尝试返回注释数组,但我在返回语句中收到了以下错误。

  

void函数中出现意外的非void值

我做了一些研究,发现我可能试图用完成处理程序返回,但似乎无法找到解决方法。

func getAnnotations() {

    //The php file connects the Database and Swift
    let request = NSMutableURLRequest(url: NSURL(string: "https://test.com/pull.php")! as URL)

    //Receiver's HTTP request method
    request.httpMethod = "POST"

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

        //Error Checking
        if error != nil {
            print("error=\(error)")
            return
        }

        //Response string built up
        let responseString = NSString(data: old_data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(responseString)")

        //Manipulating JSON data
        do {
            if let new_data = old_data, 
            let json = try JSONSerialization.jsonObject(with: new_data) as? [String: Any],
            let buildings = json["buildings"] as? [[String: Any]] {

                var annotations: [ARAnnotation] = []

                for building in buildings {

                    let BUILDING_ID = "BuildingID"
                    let BUILDING_NAME = "BuildingName"
                    let BUILDING_INFO = "BuildingInfo"
                    let BUILDING_LAT = "Latitude"
                    let BUILDING_LNG = "Longitude"

                    let IdString:String = building[BUILDING_ID] as! String
                    let NameString:String = building[BUILDING_NAME] as! String
                    let InfoString:String = building[BUILDING_INFO] as! String

                    let LatString:String = building[BUILDING_LAT] as! String
                    let LatDouble:Double = Double(LatString)!

                    let LngString:String = building[BUILDING_LNG] as! String
                    let LngDouble:Double = Double(LngString)!

                    let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(LatDouble, LngDouble)

                    let annotation = ARAnnotation()
                    annotation.location = CLLocation(latitude: LatDouble, longitude: LngDouble)
                    annotation.title = "POI: \(NameString)"
                    annotations.append(annotation)

                    return annotations
                    //error
                }  
            }     
        } catch {
            print(error)
        } 
    }
    task.resume()
}

我的这个功能的目标是能够拉出包含我的数据的变量(注释,位置,NameString,InfoString ......)并在另一个函数中使用它们。

如果有其他方式而不是简单的回报,我很乐意听到。

由于

1 个答案:

答案 0 :(得分:0)

这是一项网络任务,您不应该返回任何内容。因为加载数据需要一些时间,所以在调用函数时总是得到一个空数组。让我们使用一个闭包作为完成处理程序,如下所示:

getAnnotations() { (annotations, error) in

    // TODO: something
    print("annotations: \(annotations), error: \(error)")
}

并像这样使用它:

@api.multi
def search_partner_seniat(self):
    """ Check vat of the partner and update iva rate
    """
    self.ensure_one()
    vat = self.vat.upper()
    res = {
        'name': _('The requested contributor does not exist'),
        'vat_subjected': False,
        'vat': vat,
        'wh_iva_agent': False,
        'wh_iva_rate': 0.0
    }

    if 'VE' in vat:
        vat = vat[2:]

    # assumption: both methods in new api style
    if self.env['res.partner'].check_vat_ve(vat): 
        res = self.env['seniat.url']._dom_giver(vat)  
    self.write(res)

    return {
        'type': 'ir.actions.act_window',
        'res_model': 'search.info.partner.seniat',
        'view_mode': 'form',
        'view_type': 'form',
        'res_id': self.id,
        'views': [(False, 'form')],
        'target': 'new',
    }