__block
keyword on a variable in Objective C
used to assure that this variable will be treated specially in block
, but what is the equivalent to this keyword in swift
?
I have done some google research but everything confuses me so far.
Following is my code :
var myArr = NSMutableArray() completionBlock(myArray,nil)
I have tried
__block var myArr = NSMutableArray()
The __block Storage Type __block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable's lexical scope.
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.
Normally, variables and their contents that are also used in blocks are copied, thus any modification done to these variables don't show outside the block. When they are marked with __block , the modifications done inside the block are also visible outside of it.
There is no need for __block
in Swift. A captured variable is automatically settable from within the closure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With