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
18
votes
6 answers

Simple GCD Serial Queue example like FIFO using blocks

I read Apple documentation on how to Use serial queues to ensure that tasks to execute in a predictable order but now i am confused too much. Some how i am able to work serially but still i am not clear so i need simple serial example for my methods…
DreamWatcher
  • 399
  • 1
  • 3
  • 11
17
votes
3 answers

Block_release deallocating UI objects on a background thread

One of the patterns presented at the WWDC 2010 "Blocks and Grand Central Dispatch" talk was to use nested dispatch_async calls to perform time consuming tasks on a background thread and then update the UI on the main thread once the task is…
17
votes
5 answers

How can code inside an Objective-C block reference the block object itself?

self is merely a captured variable inside a block and doesn't reference the block itself, so how does a block reference itself without having an explicit captured variable for that purpose?
Steve Weller
  • 4,599
  • 4
  • 23
  • 30
17
votes
2 answers

UIScrollView touch events during animation not firing with animateWithDuration: but work fine with UIView beginAnimations:

I have a UIScrollView subclass that I am programmatically scrolling using UIView animations. I'd like the user to be able to tap or zoom into the UIImageView content of the Scroll View while the animation is taking place. This has worked fine while…
17
votes
1 answer

How to use "enumerateChildNodesWithName" with Swift in SpriteKit?

I'm using Swift to make a game in SpriteKit. In Objective-C I could use the following method: (void)enumerateChildNodesWithName:(NSString *)name usingBlock:(void (^)(SKNode *node, BOOL *stop))block to perform actions on that *node, but I can't get…
rv123
  • 476
  • 1
  • 3
  • 14
17
votes
1 answer

Encoding an Objective-c Block?

Is it possible to encode an Objective-C block with an NSKeyedArchiver? I don't think a Block object is NSCoding-compliant, therefore [coder encodeObject:block forKey:@"block"] does not work? Any ideas?
rebo
  • 1,067
  • 1
  • 7
  • 10
17
votes
3 answers

iOS autorelease pool blocks

I was reading the documentation from apple about memory management when I got to autorelease pool blocks and something got me thinking. Any object sent an autorelease message inside the autorelease pool block is released at the end of the…
Teo
  • 3,394
  • 11
  • 43
  • 73
17
votes
2 answers

Block recursion and breaking retain cycle

To better illustrate the question, consider the following simplified form of block recursion: __block void (^next)(int) = ^(int index) { if (index == 3) { return; } int i = index; next(++i); }; next(0); XCode (ARC-enabled)…
krisk
  • 6,957
  • 1
  • 18
  • 30
16
votes
1 answer

Block gets released whilst in NSDictionary (ARC)

I'm trying to retain a reference to a Block that's been passed in to my class by a method, to call at a later time. I'm having trouble, however, maintaining a reference to it. The obvious way, I thought, was to add it to an ivar collection, all of…
16
votes
1 answer

EXC_BAD_ACCESS when copying or retaining Block

As far as I understand a Block acts like an object, in that you can send copy or release messages to it, e.g: [myBlock copy]; However whenever I do this, or release a block, I get EXC_BAD_ACCESS. If I use the block functions, everything works as…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
16
votes
3 answers

iPhone : Unable to understand the following coding

I have downloaded some example coding. But I found strange coding or maybe I have seen it first time. Can anybody help me to understand following coding? NSArray *wordStrings = [response.spellingSuggestions wn_map: ^id (id obj) { return [obj…
Devang
  • 11,258
  • 13
  • 62
  • 100
16
votes
2 answers

Should a block literal retain referenced heap-allocated blocks

Consider the following code: // t included so block1 is a stack block. See [1] below int t = 1; SimpleBlock block1 = ^{ NSLog(@"block1, %d", t); }; // copy block1 to the heap SimpleBlock block1_copied = [block1 copy]; // block2 is allocated on the…
Andrew Hershberger
  • 4,152
  • 1
  • 25
  • 35
16
votes
1 answer

Dereferencing a __weak pointer is not allowed inside block

Apple docs say I can avoid a strong reference cycle by capturing a weak reference to self, like this: - (void)configureBlock { XYZBlockKeeper * __weak weakSelf = self; self.block = ^{ [weakSelf doSomething]; // capture the weak…
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
16
votes
3 answers

How does a Block capture the variables outside of its enclosing scope?

I know that an Objective-C Block can capture and set the value of variables outside of its enclosing scope. How does it do that?
iCanObjSeeSharp
  • 371
  • 2
  • 6
  • 13
16
votes
3 answers

Defining Objective-C blocks as properties - best practice

I've recently come across an Apple document that shows the following property declaration for a block: @interface XYZObject : NSObject @property (copy) void (^blockProperty)(void); @end Also, this article states: Note: You should specify copy as…
Stavash
  • 14,244
  • 5
  • 52
  • 80