如何调用包含完成处理程序的函数?

时间:2016-02-23 18:28:01

标签: swift2 alamofire swifty-json

由于我的大多数应用程序都使用非同步的rest api调用,所以仍然试图获得完成处理程序的挂起。我的项目使用AlamofireSwiftyJSON我有一个类函数,它接受我想用来调用POST api的参数并将值插入数据库。 API返回插入的新行的ID。我希望将响应作为从函数返回的一个快速JSON对象。这是功能:

class func insertItem(item: String, description: String, quantityOnHand: Int, reorderPoint: Int, supplier_id:Int, completionHandler: JSON -> Void) {

    let urlEndpoint = Constants.Urls.Inventory.Add
    let newItem = ["item": item, "description": description, "quantityOnHand": quantityOnHand, "reorderPoint": reorderPoint, "supplier_id": supplier_id]

    Alamofire.request(.POST, urlEndpoint, parameters: (newItem as! [String : AnyObject]), encoding: .JSON)
        .responseJSON { response in
            guard response.result.error == nil else {
                //got an error, need to handle it
                print(response.result.error!)
                return
            }

            completionHandler(JSON(response.response!))
    }

}

我试图调用此功能,但我不确定要为完成参数添加什么:

Inventory.insertItem("NewFromFunc", description: "new function!!!", quantityOnHand: 400, reorderPoint: 1, supplier_id: 2, completionHandler: ???)
    }

编辑:现在更新了功能。完成后无法返回任何内容:

    func insertItem(item: String, description: String, quantityOnHand: Int, reorderPoint: Int, supplier_id:Int, completionHandler: (JSON) -> Void) {

        let urlEndpoint = "http://url"
        let newItem = ["item": item, "description": description, "quantityOnHand": quantityOnHand, "reorderPoint": reorderPoint, "supplier_id": supplier_id]

        Alamofire.request(.POST, urlEndpoint, parameters: ((newItem) as! [String : AnyObject]))
            .responseJSON { response in
                //print(newItem)

                guard response.result.error == nil else {
                    //got an error, need to handle it
                    print("Error thread")

                    return
                }
                if let value: AnyObject = response.result.value {
                    //hande results as JSON without bunch of nested if loops
                    let statusCode = response.response?.statusCode
                    if statusCode == 201 {

                        //let newid = value["id"]
                        //let status = value["status"]

                        print("Status is 201")
                        print(value)
                    }
                    else {
                        print("there's an issue returning data")
                    }
                }
                completionHandler(JSON(response.response!))
            }
    }

2 个答案:

答案 0 :(得分:1)

您需要设置完成处理程序的类型。你有一个JSON类型的参数,所以有点像这样的

completionHandler: (JSON) -> Void

要使其成为可选项,您可以执行类似这样的操作

completionHandler: ((JSON) -> Void)?

然后你会这样打电话

Inventory.insertItem("NewFromFunc", description: "new function!!!", quantityOnHand: 400, reorderPoint: 1, supplier_id: 2){ data in 
    //Do something with the data object
}

如果您要使用具有相同类型处理程序的多个方法等,也可以通过创建typeAlias来完成此操作

以下是我在休息客户端类中使用typealias的示例,因为响应用于多个方法

typealias ServiceResponse = (JSON, NSError?) -> Void

class RESTClient : NSObject {
    static let sharedInstance = RESTClient()

    func makeRequest(type: String, endPoint: String, parameters: [String: AnyObject]?, onCompletion: ServiceResponse){
        makeRequest(type, endPoint: endPoint, parameters: parameters, files: nil, onCompletion: onCompletion)
    }
...

答案 1 :(得分:1)

您可以创建新班级

导入UIKit

typealias ServiceResponse =(NSDictionary!,NSError!) - >空隙

类AFNInjector:NSObject {

func getDataWith(parameters:NSDictionary, url:String, onCompletion: ServiceResponse) -> Void {

    let manager:AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()
    let serializer:AFJSONRequestSerializer = AFJSONRequestSerializer()
    manager.responseSerializer.acceptableContentTypes = NSSet(objects:"text/html") as NSSet as Set<NSObject>
    manager.requestSerializer = serializer
    manager.POST(url, parameters: parameters, success: { (operation: AFHTTPRequestOperation!, responsobject: AnyObject?) -> Void in
        let data = responsobject as! NSDictionary?
        onCompletion(data,nil)
    }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        let errorObject = error as NSError?
        onCompletion(nil,errorObject)
    }
}

}

之后你可以打电话给这个

    let Obj=AFNInjector()
    let parameters:NSDictionary = ["apicall":"Login","password":txtPassword.text,"email":txtEmail.text]

   Obj.getDataWith(parameters, url:BASEURL) { (result, error) -> Void in
        if (result != nil){


            print(result)


           }
        else{



        }
    }