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
25
votes
1 answer

Is there an easy way to autocomplete block variables in xcode?

Usually Xcode autocomplete works really well for me but when tabbing through method signatures with blocks and block variables I always have to retype the variable types and names. Here's an example: The animation block is easy to complete since it…
Nick
  • 9,792
  • 7
  • 50
  • 60
24
votes
5 answers

Cocoa blocks as strong pointers vs copy

I did work several times with blocks as with pointers to which i had strong reference I heard that you should use copy, but what is the implication in working with blocks as pointers and not with the raw object? I never got a complain from the…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
23
votes
2 answers

Possible to pass [self anyFunction] in blocks without __weak object (iOS 5 + ARC)

Is it possible to pass [self anyFunction] in blocks without a __weak object from self? As an example this is valid code from the System Framework: [UIView animateWithDuration:0.8 animations:^{ //Do animationStuff }…
23
votes
2 answers

avoiding "expression result unused" warning in a block

The following code is returning an expression unused warning on the assignment operation in the block. The code isn't very practical, but there is a lot more code in the excluded section and that code has to run on a particular queue. __block…
Mark Lilback
  • 1,154
  • 9
  • 21
22
votes
1 answer

How can I retrieve a return value from a completion block?

Is it possible to run a completion block on the main thread? For example, I have one method which returns a value: - (int)test { /* here one method is called with completion block with return type void */ [obj…
22
votes
3 answers

Having trouble with BOOL return type in Objective-C blocks

I stumbled over a curious problem with BOOL return type in blocks. Having the following definition: typedef BOOL (^BoolBlock)(void); …this code passes: BoolBlock foo = ^{ return YES; }; …but this fails to compile: BoolBlock bar = ^{ return YES ||…
zoul
  • 102,279
  • 44
  • 260
  • 354
22
votes
3 answers

Grand Central Dispatch (GCD) with CoreData

I'm using Grand Central Dispatch (GCD) in my application to do some heavy lifting. The application is using Core-Data for data storage purposes. Here's my scenario (along with relevant question): dispatch_queue_t main_queue =…
Mustafa
  • 20,504
  • 42
  • 146
  • 209
22
votes
1 answer

Have you noticed that dispatch_after runs ~10% too slow on iOS devices?

Lately I've been using dispatch_after instead of performSelector:withObject:afterDelay when I want to trigger some code after a delay. The code is cleaner, it has access to the enclosing scope, I can put the code in-line instead of writing a…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
22
votes
3 answers

recursive block and retain cycles in ARC

EDIT2: No. The suggested answer is about async calls. I want & need synchronous calls, like in a normal, standard recursive call. EDIT: while __unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ; compiles without warning or errors, it…
22
votes
1 answer

What will happen if I have nested dispatch_async calls?

It may be a dumb question but I need to ask and clear this up for myself. To submit a block onto a queue for execution, use the functions dispatch_sync and dispatch_async. They both take a queue and a block as parameters. dispatch_async returns…
21
votes
2 answers

using completion with animateWithDuration causes exc_bad_access

I am trying to animate 2 UIButtons in a UITableViewCell called addToPlaylist and removeFromPlayList (they animate off to the right after being swiped on) and am using a block as follows [UIView animateWithDuration:0.25 animations:^{ …
craigk
  • 1,294
  • 2
  • 12
  • 25
21
votes
2 answers

Capturing 'self' strongly in this block is likely to lead to a retain cycle

I have reqest with block. But the compiler issues a warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" __weak typeof(self) weakSelf = self; [generalInstaImage setImageWithURLRequest:[NSURLRequest…
21
votes
1 answer

How do I create an objective-c method that return a block

-(NSMutableArray *)sortArrayByProminent:(NSArray *)arrayObject { NSArray * array = [arrayObject sortedArrayUsingComparator:^(id obj1, id obj2) { Business * objj1=obj1; Business * objj2=obj2; NSUInteger prom1=[objj1…
user4951
  • 32,206
  • 53
  • 172
  • 282
20
votes
1 answer

Objective C - How to implement custom callback method but enforce specific parameter?

In my app I have a custom UITableViewCell subclass that pops up a picker when pressed. from now on I'll refer to this class as PickerCell. I'm using several of instances of PickerCell in the same UITableView. I don't want the cell to respond to the…
Avi Shukron
  • 6,088
  • 8
  • 50
  • 84
20
votes
1 answer

What is the property block declaration equivalent in swift of the following block property?

In Objective-C I do this: @property (nonatomic, copy) void(^completion)(MyObject * obj); What is the correct way to do this in swift?
zumzum
  • 17,984
  • 26
  • 111
  • 172