0

我对 XCode 和 Swift 很陌生,在我的 iOS 应用程序中,我编写了一种方法来执行对服务器上 php 文件的 POST 请求,方法是在 stackoverflow 中遵循一些答案

func closeTaskService(id_task:Int, completionHandler: (response: NSString) -> ()) {

    let request = NSMutableURLRequest(URL: NSURL(string: Settings.webServerGetUserTasks)!)
    request.HTTPMethod = "POST"
    let postParams = "action=close&id_task=\(id_task)"
    request.HTTPBody = postParams.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        println("response = \(response)")
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("responseString = \(responseString!)")

        completionHandler(response: responseString!)

    }
    task.resume()
}

在哪里

webServerGetUserTasks = "http://localhost:8888/excogitoweb/mobile/handleTasks.php"

问题是我不知道如何在我的视图控制器类上调用该函数。我见过的例子在函数中没有参数,所以当它被调用时,他们只是写了闭包。这是我到目前为止所尝试的:

self.taskService.closeTaskService(self.taskCollection[indexPath.row].id), {(response: NSString) -> () in
        println("response = \(response)")})

但是 XCode 显示了很多错误,它建议我添加 ';' 在两三个地方......请你解释一下如何调用这种函数以及如何编写闭包?

4

4 回答 4

1

首先需要了解的是Closure Expression Syntax是如何定义的,根据Apple的闭包表达式语法有以下一般形式:

{ (parameters) -> return type in
    statements
}

根据苹果

如果您需要将闭包表达式作为函数的最终参数传递给函数并且闭包表达式很长,则将其编写为尾随闭包会很有用。尾随闭包是一个闭包表达式,它写在它支持的函数调用的括号之外(和之后):

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
}

// here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure({
    // closure's body goes here
})

// here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
    // trailing closure's body goes here
}

然后,我认为通过上面的答案和这个答案,您可以更好地理解闭包是如何工作的以及如何调用它。不过,我强烈建议您阅读 Apple 书籍的Closures章节,以深入了解它的工作原理。

我希望这对你有帮助。

于 2015-09-03T19:15:27.890 回答
1

尝试这个

self.taskService.closeTaskService(self.taskCollection[indexPath.row].id), completionHandler: {(response: NSString) -> () in
            println("response = \(response)")
})
于 2015-09-03T19:08:52.267 回答
1

我猜是一个简单的拼写错误。当我使用完成处理程序调用函数时,我总是让自动完成来完成它的工作,然后双击周围有圆框的东西。这总是给出正确的语法。

self.taskService.closeTaskService(self.taskCollection[indexPath.row].id) { (response) -> () in
    print(response)
}
于 2015-09-03T19:02:14.703 回答
1

尝试这个

func closeTaskService(id_task:Int, completionHandler: (response: NSString) -> ()) {
    let request = NSMutableURLRequest(URL: NSURL(string: "http://myserverip/myfile.php")!)
    request.HTTPMethod = "POST"
    let postParams = "action=close&id_task=\(id_task)"
    request.HTTPBody = postParams.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        println("response = \(response)")
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("responseString = \(responseString!)")
        completionHandler(response: responseString!)
    }
    task.resume()
}

并像这样调用你的方法

self.closeTaskService(12, completionHandler: { (response) -> () in
    //handle response
})
于 2015-09-03T19:06:00.767 回答