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

How does typedef-ing a block works

In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear. Doing typedef for a block -typedef void (^MyBlock) (int a); Now, we can use MyBlock. Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define? How the…
user1559227
  • 1,021
  • 2
  • 10
  • 13
49
votes
1 answer

Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)"

In My dispach_async code block I cannot access global variables. I am getting this error Variable is not Assignable (missing _block type specifier). NSString *textString; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, …
47
votes
6 answers

Can AFNetworking return data synchronously (inside a block)?

I have a function using AFJSONRequestOperation, and I wish to return the result only after success. Could you point me in the right direction? I'm still a bit clueless with blocks and AFNetworking specifically. -(id)someFunction{ __block id…
Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
47
votes
5 answers

How to write an Objective-C Completion Block

I'm in a situation where need to call a class method from my view controller, have it do it's thing, but then perform some actions ONLY AFTER the class method has completed. (I think what I need is a completion block, but please correct me if I'm…
Andrew
  • 1,344
  • 1
  • 12
  • 20
45
votes
2 answers

Block references as instance vars in Objective-C

I was wondering if it's possible to store a reference to an anonymous function (block) as an instance variable in Objective-C. I know how to use delegation, target-action, etc. I am not talking about this.
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
44
votes
3 answers

Objective-C: `continue` in collection enumeration block?

If I have an NSArray and I use enumerateUsingBlock to loop through elements in the array, but in some cases I need to skip the loop body and go to next element, is there any continue equivalent in block, or can I use continue…
hzxu
  • 5,753
  • 11
  • 60
  • 95
42
votes
3 answers

iOS blocks and strong/weak references to self

I have a question about strong and weak references to self in blocks in iOS. I know the proper way to refer to self inside a block is to create a weak reference outside the block, and then a strong reference to that weak reference inside the block,…
Mason
  • 6,893
  • 15
  • 71
  • 115
42
votes
2 answers

Is it necessary to use weak references to self always inside blocks..?

I am getting confused with use of self inside blocks, I go through some of Apple's documents but still cannot find the right answer. Some people always say use weak self inside blocks, but some say use weak self in blocks that are copied, not…
ShivaPrasad
  • 915
  • 1
  • 11
  • 22
40
votes
4 answers

Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController?

Say I have the following method inside a UIViewController subclass: - (void)makeAsyncNetworkCall { [self.networkService performAsyncNetworkCallWithCompletion:^{ dispatch_async(dispatch_get_main_queue(), ^{ …
40
votes
2 answers

How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on…
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
37
votes
3 answers

Executing Blocks From NSArray?

I was just thinking, as you can treat Blocks like objects if I create two of them and then add them to an NSArray is there a way to execute them from the array? int (^Block_001)(void) = ^{ return 101; }; int (^Block_002)(void) = ^{ return 202;…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
37
votes
6 answers

Better asynchronous control flow with Objective-C blocks

I'm using AFNetworking for asynchronous calls to a web service. Some of these calls must be chained together, where the results of call A are used by call B which are used by call C, etc. AFNetworking handles results of async calls with…
36
votes
1 answer

Objective-C callback handler

I have a callback method that I got to work, but I want to know how to pass values to it. What I have is this: @interface DataAccessor : NSObject { void (^_completionHandler)(Account *someParameter); } - (void) signInAccount:(void(^)(Account…
Jesse
  • 891
  • 1
  • 13
  • 22
34
votes
4 answers

Objective-C Block Property with Xcode code completion

Is it possible to define an Objective-C block property but still have full-code completion in Xcode 4? If I use a typedef to define the block: typedef void (^CompletionBlock)(MyObject *myObj); and then define the property: @property (nonatomic,…
Andrew
  • 7,630
  • 3
  • 42
  • 51
34
votes
5 answers

Why should I choose GCD over NSOperation and blocks for high-level applications?

Apple's Grand Central Dispatch reference says: "...if your application needs to operate at the Unix level of the system—for example, if it needs to manipulate file descriptors, Mach ports, signals, or timers. GCD is not restricted to…
Lio
  • 4,225
  • 4
  • 33
  • 40