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

How to store blocks in properties in Objective-C?

I'd like to store objective-c block in a property for later use. I wasn't sure how to do it so I googled a bit and there is very little info about the subject. But I've managed to find the solution eventually and I've thought that it might be worth…
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
79
votes
3 answers

Custom completion block for my own method

I have just discovered completion blocks: completion:^(BOOL finished){ }]; What do I need to do to have my own method take a completion block?
user2206906
  • 1,310
  • 2
  • 13
  • 18
74
votes
4 answers

Why do nil / NULL blocks cause bus errors when run?

I started using blocks a lot and soon noticed that nil blocks cause bus errors: typedef void (^SimpleBlock)(void); SimpleBlock aBlock = nil; aBlock(); // bus error This seems to go against the usual behaviour of Objective-C that ignores messages to…
zoul
  • 102,279
  • 44
  • 260
  • 354
73
votes
6 answers

Calling [self methodName] from inside a block?

I've just run into blocks and I think they are just what I'm looking for, except for one thing: is it possible to call a method [self methodName] from within a block? This is what I'm trying to do: -(void)someFunction{ Fader* fader = [[Fader…
Marty
  • 1,077
  • 1
  • 10
  • 14
72
votes
3 answers

Implementing a method taking a block to use as callback

I would like to write a method similar to this: +(void)myMethodWithView:(UIView *)exampleView completion:(void (^)(BOOL finished))completion; I've basically stripped down the syntax taken from one of Apple's class methods for UIView: +…
Chris
  • 2,478
  • 3
  • 23
  • 38
69
votes
5 answers

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

Given the following: - (void) someMethod { dispatch_async(dispatch_get_main_queue(), ^{ myTimer = [NSTimer scheduledTimerWithTimeInterval: 60 target: self …
Kyle
  • 17,317
  • 32
  • 140
  • 246
65
votes
10 answers

UIButton block equivalent to addTarget:action:forControlEvents: method?

I looked around, but couldn't find this on the internet, nor anywhere in the Apple docs, so I'm guessing it doesn't exist. But is there a iOS4 blocks equivalent API to: [button addTarget:self action:@selector(tappy:)…
Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
65
votes
3 answers

How to dispatch on main queue synchronously without a deadlock?

I need to dispatch a block on the main queue, synchronously. I don’t know if I’m currently running on the main thread or no. The naive solution looks like this: dispatch_sync(dispatch_get_main_queue(), block); But if I’m currently inside of a block…
zoul
  • 102,279
  • 44
  • 260
  • 354
64
votes
1 answer

Which is the right one, nil or NULL, to mark "no Objective-C block"?

If I want to pass nothing for an Objective-C block, what keyword should I use, NULL or nil? I'm asking this because an Objective-C block is an Objective-C object (as I know), but represented as a function pointer. NULL and nil both indicate a 0x0…
eonil
  • 83,476
  • 81
  • 317
  • 516
61
votes
2 answers

Using weak self in dispatch_async function

I read a lot of posts about using __weak self inside dispatch_async, and now I am a litle bit confused. if I have : self.myQueue = dispatch_queue_create("com.biview.core_data", NULL); dispatch_async(self.myQueue, ^(void){ if (!self.var1) { …
Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
60
votes
6 answers

Why can't we use a dispatch_sync on the current queue?

I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn't know which until runtime (using StoreKit.framework). I also had UI code that I needed to update in that callback…
53
votes
3 answers

dispatch_sync vs. dispatch_async on main queue

Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity…
52
votes
2 answers

Storing Blocks in an Array

In Objective-C, I know that blocks are considered objects, so I was wondering if it was possible to store them in an array. This begs the question, are blocks first class objects or are they just treated like objects for the sake of passing them…
noizetoys
  • 2,923
  • 5
  • 24
  • 15
50
votes
4 answers

How to cancel NSBlockOperation

I have a long running loop I want to run in the background with an NSOperation. I'd like to use a block: NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while(/* not canceled*/){ //do something... } }]; The…
jemmons
  • 18,605
  • 8
  • 55
  • 84
49
votes
3 answers

Difference between block (Objective-C) and closure (Swift) in iOS

In tutorials it's written that functionally both are same even closure is more easier then block and its avoided the complexity of block and memory management, I've gone through many tutorials but except these I'm not getting the difference between…
Sujay
  • 2,510
  • 2
  • 27
  • 47