Higher-Order Functions in Swift, Episode 2: Write a Higher-Order Function | raywenderlich.com

Write your first higher-order function, using a function as a parameter. Learn three ways to pass functions in as arguments.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/23523624-higher-order-functions-in-swift/lessons/2

Hi Catie, I need to understand better. My task is to get a token (which means waiting for a urlSession to return and then calling a second API to do the work. Something like this:

func outerFunction (_ action: innerFunction) {
get Token() { token in
   call innerFunction
}

func innerFunction(completion: @escaping (someProtocol)) -> Void {}

How do I do this?
Thanks,
Dan

I think I’m almost there. Here is what I have:

     @IBAction func connectTapped(_ sender: Any) {
        var aPIManager = APIManager(corporateAccountNumber: ”accountNumber”)
        aPIManager.perform { connectRosters in
            aPIManager.getConnectRosters { connectRosters in
            }
        }
    }
 
 
    mutating func perform(_ action: @escaping (_ connectProtocol: ConnectProtocol) -> Void) {
        something.performAction { (accessToken, _, error) in
 
            guard error == nil, let token = accessToken else {
                alertError()
                return
            }
 
            if let accessToken = accessToken {
                self.token = accessToken
                action() // COMPILER THINKS I NEED AN ARGUMENT HERE
            }
        }
    }