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
34
votes
3 answers

How to simplify callback logic with a Block?

Let's say I need to communicate with a class that provides a protocol and calls delegate methods when an operation is complete, as so: @protocol SomeObjectDelegate @required - (void)stuffDone:(id)anObject; - (void)stuffFailed; @end @interface…
Alan Zeino
  • 4,406
  • 2
  • 23
  • 30
34
votes
10 answers

Generic typeof for weak self references

I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles. When I first read about this it seems that the convention was to use __block typeof(self) bself = self;, which compiles but…
33
votes
2 answers

Alternative syntax to __block?

I have question on the syntax of __block variables. I know you can use __block on a variable in scope so it's not read-only inside the block. However in one spot in the apple docs, I saw an alternative: "Variables in the defining scope are…
pk-nb
  • 2,850
  • 1
  • 18
  • 20
33
votes
1 answer

FIFO serial queue using GCD

I am trying to create a (network) synchronized array for the company I work for. While the networking part works fine, I have dwelled into an issue. My wish was to create a new queue using dispatch_create_queue, to which I would add two blocks that…
Ælex
  • 14,432
  • 20
  • 88
  • 129
29
votes
4 answers

Correct management of addObserverForName:object:queue:usingBlock:

I'm still new to blocks in objective-c and wondering if I have this psuedo code correct. I'm not sure if it's enough to just remove the observer or if i have to call removeObserver:name:object: -(void) scan { Scanner *scanner = [[Scanner alloc]…
seanalltogether
  • 3,542
  • 3
  • 26
  • 24
29
votes
3 answers

Do we need to use __weak self inside UIAnimationBlocks in ARC?

Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak? [UIView animateWithDuration:animationDuration delay:0 …
arango_86
  • 4,236
  • 4
  • 40
  • 46
29
votes
7 answers

Objective-C crash on __destroy_helper_block_

I have an iOS application that's crashing on calls like __destroy_helper_block_253 and __destroy_helper_block_278 and I'm not really sure what either "destroy_helper_block" is referencing or what the number after it is supposed to point to. Does…
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
28
votes
5 answers

Checking Objective-C block type?

This is primarily a curiosity, I'm not really sure what's the practical use of this but here goes. Since blocks are also Objective-C objects, is it possible to check their type? That is, does it respond to the isKindOfClass: message and how to use…
adib
  • 8,285
  • 6
  • 52
  • 91
28
votes
3 answers

Should I still copy/Block_copy the blocks under ARC?

I've just stumbled over the following SO topic: Why should we copy blocks rather than retain? which has the following sentence: However, as of iOS 6 they are treated as regular objects so you don't need to worry. I was really confused by this…
27
votes
3 answers

Objective C — What is the fastest and most efficient way to enumerate an array?

Edit I read through some articles on blocks and fast enumeration and GCD and the like. @Bbum, who's written many articles on the subject of GCD and blocks, says that the block enumeration methods are always as fast or faster than the fast…
27
votes
2 answers

Using __block and __weak

I've read over this thread: What does the "__block" keyword mean? which discusses what __block is used for but I'm confused about one of the answers. It says __block is used to avoid retain cycles, but the comments underneath it leave me unsure. I'm…
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
26
votes
2 answers

ARC, ivars in Blocks and Reference Cycles via Captured Self

I’m working in a pure iOS5/ARC environment, so I can use __weak references as needed. I do reference ivars in a block in many situations, most notably, animation blocks that move views around, which are properties of say, my view controller…
idStar
  • 10,674
  • 9
  • 54
  • 57
26
votes
3 answers

Reloading a UICollectionView using reloadData method returns immediately before reloading data

I need to know when reloading a UICollectionView has completed in order to configure cells afterwards (because I am not the data source for the cells - other wise would have done it already...) I've tried code such as [self.collectionView…
26
votes
3 answers

What is the purpose of using blocks

I want to use blocks in my application, but I don't really know anything about blocks. Can anyone explain how and why I should use blocks in my code?
Ben10
  • 3,221
  • 2
  • 34
  • 61
25
votes
6 answers

Make iOS blocks execute synchronously

How can I make a block execute synchronously, or make the function wait for the handler before the return statement, so the data can be passed back from the block? -(id)performRequest:(id)args { __block NSData *data = nil; [xyzclass…
Sathya
  • 2,197
  • 4
  • 22
  • 25