swiftcompletion-block

Completion blocks return nothing


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        isSuccess(true, success: { (name) -> String in
            return "My name is \(name)"
        })

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }


    func isSuccess(val:Bool, success: (name: String) -> String) {
        if val {
            success(name: "Jacky")
        }
    }

}

I expect it to return string "My name is Jacky",but it didn't .But if I change the isSuccess to :

    func isSuccess(val:Bool, success: (name: String) -> String) {
        if val {
            print(success(name: "Jacky"))
        }
    }

}

Then it worked properly, why is that? Thanks in advance!


Solution

  • Your completion block returns a String. When you invoke it by calling

    success(name: "Jacky")
    

    the completion block returns the String My name is Jacky. But you do nothing with that string. You just returned it and never used it. In your second example, you actually used it - you took the string from the completion block, and printed it.

    For example, instead the print, you could also write

    let stringFromCompletionBlock = success(name: "Jacky")
    

    That way you can see that it indeed returned a value.


    And another thing is that the completion block should be call as the last thing in the function - this way you "notify" that the function has finished it's purpose, so it's not reasonable to use the value returned from a completion block inside the same function which called that completion block