Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture List and Function passed as argument in swift

Tags:

swift

I have a function with this prototype:

func myFunction(completionHandler:((response:[NSString:AnyObject])->Void)?))

The completionHandler prototype can be passed as closure or as a function... passing it as closure I know how to define a capture list with this syntax [weak self], but how can I define a capture list if instead of defining the closure directly in the function argument I want to pass a function name?

myFunction(anotherFunction) // how to set capture list from here?

VS

myFunction{ 
     [weak self] (response) in

}

```

EDIT-----

A possible solution is to wrap the function code into a block, set the capture list and execute the block... but it sounds so strange :/

myFunction(anotherFunction) // how to set capture list from here?
.
.
.
func anotherFunction(response:[NSString:AnyObject]) { 
    let safeBlock = { 
       [weak self] in { 
         self?.callSomethingSafely()
       }
    }

    safeBlock()
}
like image 651
MatterGoal Avatar asked Aug 19 '16 15:08

MatterGoal


People also ask

What is captured list in Swift?

A capture list is defined after the opening curly brace and before the parameter list and return type of the closure. An element of a capture list is the weak or unowned keyword followed by the reference to a class instance. In this example, we use a capture list to weakly capture self in the closure.

How do you pass a function as a parameter in Swift?

To pass a function as a parameter into another function in Swift, the accepted parameter type needs to describe a function. Now you can call this function by passing it an argument that is any function that takes two doubles and returns a double. This is the quick answer.

What is Clouser in Swift?

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

What is capturing values in Swift?

If you use any external values inside your closure, Swift captures them – stores them alongside the closure, so they can be modified even if they don't exist any more.


1 Answers

EDIT (based on correction from Hamish below):

I'm not aware of a way to force a referenced method to internally use a weak version of a captured variable. As you have in your example, it's up to to the actual method implementation to do that or not.

However, it's worth noting that unless you are going to be storing a reference to that completion handler indefinitely, you don't need to worry about weak self.

I assume that completion handler will be called as soon as some asynchronous task that myFunction kicks off is completed. In that case, any references captured by the completion handler will only be held until the completion handler runs. Then those references will be released and there won't be any retain cycles. Weak self in closures in only important if "self" retains the closure and the closure captures "self", setting up a circular reference / retain cycle.

like image 91
Daniel Hall Avatar answered Sep 22 '22 22:09

Daniel Hall