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
20
votes
2 answers

Creating an IMP from an Objective-C block

The IMP type in Objective-C represents a function pointer, as far I as understand. Is there any way to make an IMP from a block pointer? Thanks for your ideas.
20
votes
6 answers

Recursive Block Retain Cycles

Will this lead to any sort of retain cycle? Is it safe to use? __block void (^myBlock)(int) = [^void (int i) { if (i == 0) return; NSLog(@"%d", i); myBlock(i - 1); } copy]; myBlock(10); myBlock = nil;
20
votes
2 answers

Under ARC, are Blocks automatically copied when assigned to an ivar directly?

Assume the following code under ARC, typedef void (^MyResponseHandler) (NSError *error); @interface MyClass : NSObject { MyResponseHandler _ivarResponseHandler; } - (void)myMethod:(MyResponseHandler)responseHandler { _ivarResponseHandler =…
19
votes
3 answers

Meaning of symbol ^ in Objective C

Possible Duplicate: Caret in objective C What does this ^ syntax mean in Objective-C? I am tired by searching the meaning of symbol ^ in Objective C. I have seen it in lot of projects especially in back ground running tasks. I will put a link…
rakeshNS
  • 4,227
  • 4
  • 28
  • 42
19
votes
2 answers

Pass a block to a C++ method from objective C

I have a C++ helper class that I use with objective-C. I would like to pass the c++ class a block from a view controller (a callback) so that when it is executed I am on the main thread and can update the UI. I currently have a similar system…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
19
votes
2 answers

iOS 4 blocks and retain counts

I'm just getting started with blocks and Grand Central Dispatch. I've been told (and read in the Apple Documentation) that any object referenced from within a block gets retained. For instance: ^{ self.layer.transform =…
samvermette
  • 40,269
  • 27
  • 112
  • 144
19
votes
2 answers

Access C Array within blocks (variable array count) Objective-C

Blocks are fine but what about writing C arrays? Given this simplified situation: CGPoint points[10]; [myArray forEachElementWithBlock:^(int idx) { points[idx] = CGPointMake(10, 20); // error here // Cannot refer to declaration with an array…
user207616
19
votes
4 answers

Copying blocks (ie: copying them to instance variables) in Objective-C

I'm trying to understand blocks. I get how to use them normally, when passed directly to a method. I'm interested now in taking a block, storing it (say) in an instance variable and calling it later. The blocks programming guide makes it sound like…
RyanWilcox
  • 13,890
  • 1
  • 36
  • 60
19
votes
4 answers

Is it ever Ok to have a 'strong' reference for a delegate?

I have a class that retrieves JSON from a URL and returns the data via the protocol/delegate pattern. MRDelegateClass.h #import @protocol MRDelegateClassProtocol @optional - (void)dataRetrieved:(NSDictionary *)json; -…
19
votes
3 answers

How to implement an NSRunLoop inside an NSOperation

Im posting this question because I have seen a lot of confusion over this topic and I spent several hours debugging NSOperation subclasses as a result. The problem is that NSOperation doesnt do you much good when you execute Asynchronous methods…
deleted_user
  • 3,817
  • 1
  • 18
  • 27
19
votes
4 answers

Selectors or Blocks for callbacks in an Objective-C library

Question We're developing a custom EventEmitter inspired message system in Objective-C. For listeners to provide callbacks, should we require blocks or selectors and why? Which would you rather use, as a developer consuming a third party library? …
jimbo
  • 11,004
  • 6
  • 29
  • 46
18
votes
4 answers

ObjectiveC blocks Java equivalent

There's a feature of the Apple Objective-C language which is really useful to me: I can pass code blocks as argument in methods. I'd like to do that in Java, too. Something like: myManager.doSomethingInTransaction(function() { dao.save(); …
Fabio B.
  • 9,138
  • 25
  • 105
  • 177
18
votes
4 answers

Is there a SELF pointer for blocks?

I'd like to recursively call a block from within itself. In an obj-c object, we get to use "self", is there something like this to refer to a block instance from inside itself?
Arlen Anderson
  • 2,486
  • 2
  • 25
  • 36
18
votes
3 answers

Is it possible to compare two Objective-C blocks by content?

float pi = 3.14; float (^piSquare)(void) = ^(void){ return pi * pi; }; float (^piSquare2)(void) = ^(void){ return pi * pi; }; [piSquare isEqualTo: piSquare2]; // -> want it to behave like -isEqualToString...
18
votes
1 answer

How does a completion handler work on iOS?

Im trying to understand completion handlers & blocks. I believe you can use blocks for many deep programming things without completion handlers, but I think i understand that completion handlers are based on blocks. (So basically completion…
marciokoko
  • 4,988
  • 8
  • 51
  • 91