0

私はXCodeとSwiftに非常に慣れていません.iOSアプリでは、スタックオーバーフローでいくつかの回答に従って、サーバー上のphpファイルへのPOSTリクエストを実行するメソッドを作成しまし:

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"

問題は、View Controller クラスでその関数を呼び出す方法がわからないことです。私が見た例では、関数にパラメーターがないため、呼び出されたときに単にクロージャーを記述します。これが私が今まで試したことです:

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

しかし、XCodeには多くのエラーが表示され、「;」を追加するよう提案されています 2 か 3 か所で...この種の関数の呼び出し方法とクロージャの書き方を教えてください。

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 book のClosuresの章を読んで、それがどのように機能するかを深く理解することを強くお勧めします。

これがお役に立てば幸いです。

于 2015-09-03T19:15:27.890 に答える
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 に答える
1

これを試して

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