Pass swift func result to func

First off, I'm learning Swift still while working on my project and am not very good yet.

I'm trying to pass the "result" variable from a func call to another function with some of my parameters...

I'm also working with AWS's Amplify as you'll see here but that part is a bit irrelevant.

The code before is like this...

	Amplify.API.get (request:request)
	{
		result in switch result

What I would like to do is to pass that result to a func along with some other parameters.. Such as..

	Amplify.API.get (request:request)
	{
		myFunction(result, myParam1, myParam2)
  }

But Swift does things different than I'm used to with Objective-C.

The definition for Amplify.API.get is...

Amplify.API.get(request: RESTRequest, listener:((AmplifyOperation<RESTOperationRequest, Data, APIError>.OperationResult) -> Void)

How do I setup my func so that it will take in what the get function is sending and handle it? Something like...

func handleGet (request: RESTRequest, listener:((AmplifyOperation<RESTOperationRequest, Data, APIError>.OperationResult) -> Void, myParam1, myParam2)

But then I don't know how to pass that to the get or call it within the code block?

Definition of API seems incorrect:

Amplify.API.get(request: RESTRequest, listener:((AmplifyOperation<RESTOperationRequest, Data, APIError>.OperationResult) -> Void)

There seems to be an extra ( after listener.

Sorry, I copied the autofill.. Here's the definition...

func get(request: RESTRequest, listener: RESTOperation.ResultListener?) -> RESTOperation

And here's "RestOperation.ResultListener...

open class AmplifyOperation<Request: AmplifyOperationRequest, Success, Failure: AmplifyError>: AsynchronousOperation {

    /// The concrete Request associated with this operation

    public typealias Request = Request

    /// The concrete Success type associated with this operation

    public typealias Success = Success

    /// The concrete Failure type associated with this operation

    public typealias Failure = Failure

    /// Convenience typealias defining the `Result`s dispatched by this operation

    public typealias OperationResult = Result<Success, Failure>

    /// Convenience typealias for the `listener` callback submitted during Operation creation

    public typealias ResultListener = (OperationResult) -> Void

Hoping that helps clarify things? I didn't post all the code as it's not mine...

So you need to pass a closure of the same type.

In the closure code, you can use your param to do what you need to do.

Thanks @Claude31. Any example code you can give here would be much appreciated.. For more clarity, currently my code looks like this...

Amplify.API.get (request:request)
{
    result in switch result
    {
    case .success(let data):
        // Do success stuff
        
    case .failure(let apiError):
        // Do failure stuff
    }
}

I'm looking to handle that switch block in another function. So I need to change the above to something like this below (with obviously the correct syntax. Example code here would be helpful...

Amplify.API.get (request:request)
{
    myFunction(result, myParam1, myParam2)
}

It believe it may have something to do with the listener parameter from the Amplify.API.get function definition but I'm not sure how to do that while still adding in my parameters to my function call.

First of all, you should better not put the parameter declarations directly ahead of some statement in a closure:

        Amplify.API.get(request: request) {result in
            switch result {
            case .success(let data):
                // Do success stuff
            case .failure(let apiError):
                // Do failure stuff
            }
        }

And you will see you can do it in the same way:

        Amplify.API.get(request: request) {result in
            myFunction(result, myParam1, myParam2)
        }
Pass swift func result to func
 
 
Q