Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__block in Swift [closed]

Tags:

ios

swift

__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() 
like image 852
Arun Kumar Avatar asked Sep 24 '16 04:09

Arun Kumar


People also ask

What does __ block do?

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.

What are the blocks 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 keyword is used to specify a variable may be modified inside a block?

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.


1 Answers

There is no need for __block in Swift. A captured variable is automatically settable from within the closure.

like image 167
matt Avatar answered Oct 09 '22 03:10

matt