Questions tagged [objective-c-blocks]

Blocks are Apple’s implementation of closures for C, which are also available for Objective-C and C++.

Blocks are an Apple extension to C, often used in conjunction with Objective-C. When used from Objective-C, blocks also function as full Objective-C objects.
They are more traditionally known as closures. As closures, they can capture variables from the surrounding scope, be passed to functions, and be stored in variables. The syntax of a Block type is nearly identical to a function pointer, with * being replaced by ^.

An example of block definition is the following one:

int (^minusOne)(int);

minusOne = ^(int anInt) {
    return anInt - 1;
};

For more examples regarding how to declare various types of blocks, see How Do I Declare A Block In Objective-C?, and for more in-depth information, see Blocks Programming Topics.

2629 questions
773
votes
20 answers

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float?
Egil
  • 8,101
  • 4
  • 17
  • 6
477
votes
8 answers

What does the "__block" keyword mean?

What exactly does the __block keyword in Objective-C mean? I know it allows you to modify variables within blocks, but I'd like to know... What exactly does it tell the compiler? Does it do anything else? If that's all it does then why is it…
mjisrawi
  • 7,846
  • 3
  • 24
  • 27
404
votes
8 answers

Assign a variable inside a Block to a variable outside a Block

I'm getting an error Variable is not assignable (missing __block type specifier) on the line aPerson = participant;. How can I make sure the block can access the aPerson variable and the aPerson variable can be returned? Person *aPerson =…
tommi
  • 6,883
  • 8
  • 37
  • 60
330
votes
8 answers

Can I use Objective-C blocks as properties?

Is it possible to have blocks as properties using the standard property syntax? Are there any changes for ARC?
gurghet
  • 7,591
  • 4
  • 36
  • 63
281
votes
7 answers

Block Declaration Syntax List

Block syntax in Objective C (and indeed C, I presume) is notoriously incongruous. Passing blocks as arguments looks different than declaring blocks as ivars, which looks different than typedefing blocks. Is there a comprehensive list of…
Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
221
votes
8 answers

How do I avoid capturing self in blocks when implementing an API?

I have a working app and I'm working on converting it to ARC in Xcode 4.2. One of the pre-check warnings involves capturing self strongly in a block leading to a retain cycle. I've made a simple code sample to illustrate the issue. I believe I…
XJones
  • 21,959
  • 10
  • 67
  • 82
202
votes
10 answers

Waiting until two async blocks are executed before starting another block

When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that? We tried the following, but it doesn't seem to…
tom
  • 14,273
  • 19
  • 65
  • 124
169
votes
9 answers

Retain cycle on `self` with blocks

I'm afraid this question is pretty basic, but I think it's relevant to a lot of Objective-C programmers who are getting into blocks. What I've heard is that since blocks capture local variables referenced within them as const copies, using self…
Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
158
votes
8 answers

Store a closure as a variable in Swift

In Objective-C, you can define a block's input and output, store one of those blocks that's passed in to a method, then use that block later: // in .h typedef void (^APLCalibrationProgressHandler)(float percentComplete); typedef void…
Jay Dub
  • 1,682
  • 2
  • 11
  • 7
151
votes
6 answers

When to use enumerateObjectsUsingBlock vs. for

Besides the obvious differences: Use enumerateObjectsUsingBlock when you need both the index and the object Don't use enumerateObjectsUsingBlock when you need to modify local variables (I was wrong about this, see bbum's answer) Is…
Paul Wheeler
  • 18,988
  • 3
  • 28
  • 41
150
votes
11 answers

Objective-C pass block as parameter

How can I pass a Block to a Function/Method? I tried - (void)someFunc:(__Block)someBlock with no avail. ie. What is the type for a Block?
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
148
votes
5 answers

Declare a block method parameter without using a typedef

Is it possible to specify a method block parameter in Objective-C without using a typedef? It must be, like function pointers, but I can't hit on the winning syntax without using an intermediate typedef: typedef BOOL (^PredicateBlock_t)(int); -…
Bogatyr
  • 19,255
  • 7
  • 59
  • 72
104
votes
7 answers

Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called. For the latter I always used dispatch_get_current_queue(), but…
cfischer
  • 24,452
  • 37
  • 131
  • 214
88
votes
6 answers

Blocks instead of performSelector:withObject:afterDelay:

I often want to execute some code a few microseconds in the future. Right now, I solve it like this: - (void)someMethod { // some code } And this: [self performSelector:@selector(someMethod) withObject:nil afterDelay:0.1]; It works, but I have…
Rits
  • 5,105
  • 4
  • 42
  • 53
81
votes
4 answers

What is the difference between a __weak and a __block reference?

I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf = self; [self methodThatTakesABlock:^ { [tmpSelf doSomething]; }]; The following is copied from the documentation: A block forms a strong…
1
2 3
99 100