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
1
vote
1 answer

In AFNetworking, why are some return objects declared as __block type?

AFNetworking code has a few places in which __block is used for objects in methods where there is no obvious need to change the object. For example, In AFHTTPSessionManager, the GET call uses __block on the task object. Any idea why? -…
Boon
  • 40,656
  • 60
  • 209
  • 315
1
vote
1 answer

NSAnimationContext setCompletionHandler not working

setCompletionHandler method from NSAnimationContext is not working for me. I am using code form Apple's documentation: [NSAnimationContext setCompletionHandler:^{ // This block will be invoked when all of the animations // started below…
Wojtek
  • 1,006
  • 11
  • 30
1
vote
2 answers

set text of UILabel from block does not work as expected

I'm trying to set the text of a UILabel from within a code block: PLSRootViewController * __weak weakSelf = self; [self.analytics.client setMessageHandler:^(MQTTMessage *message) { NSArray *beacons = [NSJSONSerialization…
Roland
  • 9,321
  • 17
  • 79
  • 135
1
vote
3 answers

When accessing properties, what is the different between dot syntax and "->"

I have a block that uses a weak reference of itself to access properties inside the block. When accessing those properties, I use __weak ViewController *weakSelf = self; someBlock = ^{ ViewController *safeSelf =…
Tony
  • 656
  • 8
  • 20
1
vote
2 answers

Animate colour changes of background to different View Controllers at same time

I have 3 different ViewControllers that are inside a combination of methods to get one result. During the process I need to change smoothly with some kind of animation the background colour dynamically to show possible different user behaviours. The…
Alex Delgado
  • 984
  • 11
  • 19
1
vote
4 answers

Inline Block With Return Type

Is it possible in Objective-C to create a block inline and use its return type? For example, can I create a block that returns a BOOL, have it be inline, and use its return type for an assignment. BOOL b = { //…
Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
1
vote
2 answers

dispatch_group_t issue, dispatch_group_notify is calling back before leaving the group

I have the following snippet of code below that fetches data from Parse using PFQueues in the background and returns data and a status. This structure is based off of waiting for the dispatch_group_t to notify that's it's completed all entered…
1
vote
1 answer

Change a Segmented Control by Buttons

I need four buttons besides a four Labels instead of a Segmented Control with 4 buttons in the bottom of the VC. This is the code for the Segmented Control, I don't know how to set a button to activate the label to be populated by the datePicker.…
Manolo
  • 469
  • 6
  • 20
1
vote
1 answer

iOS: Waiting for API Completion Block and returning the result

Using inheritance. I have a child class that calls a method in the parent class that runs calls the server API. -(IBAction)buttonPressed { [self methodInParentClassThatCallsTheAPI:param]; // This is where I would like the call back if…
user1107173
  • 10,334
  • 16
  • 72
  • 117
1
vote
3 answers

Return a value from a block

I have been reading up on Objectice-C blocks as I have been running into them more and more lately. I have been able to solve most of my asynchronous block execution problems, however I have found one that I cannot seem to fix. I thought about…
Andrew
  • 479
  • 1
  • 5
  • 13
1
vote
0 answers

Can XCTest (the built in unit tests) be used with NSURLConnection's sendAsynchronousRequest:queue:completionHandler:?

I created a Server class with methods that use NSURLConnection's sendAsynchronousRequest:queue:completionHandler: method. I set completionHandler: to a block that should run when the server returns, and in that block I try to use XCTAssertTrue but…
1
vote
3 answers

Objective C Block Not Equal to Its Own Copy

In my app, I associate an NSTimer with a block passed to a method; the block is also added to a an array of blocks. When the timer fires, its associated block is called and should be removed from the array. So my setup looks like this: @interface…
Austin
  • 5,625
  • 1
  • 29
  • 43
1
vote
4 answers

Using Grand Central Dispatch on iOS, what queue (if any) do regular Objective-C blocks run on if they are not in a dispatch queue?

Does Option #1 below run some sort of implied queue? It does not seem to run on the main queue because when I tried to update UI stuff there it complained until I moved to Option #3 so I’m assuming that blocks have their own queue or thread? …
1
vote
2 answers

Why would I use an NSInvocation instead of a Block?

Can someone offer a concrete reason for using an NSInvocation instead of just using a Block, or even a regular method call? I am finding descriptions of NSInvocation around the Web, but no examples of where it is vital to use or even the only good…
user255468
  • 33
  • 1
  • 7
1
vote
2 answers

Why view isn't updating even on my thread?

I'm trying to realised why view isn't updating when some value is set. Problem looks as trivial one but I spent a lot of time to find the problem - with no results. When last value from the block is returned view is updated to this last reported…
Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79