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

Any simple way to add own command to a completion block?

I have a method I'm writing called "animatePulseAtSpeed: completion:" which takes a completion block which will be used in an UIView "animateWithDuration: delay: options: animations: completion:" call inside this method as shown below. The problem…
Samuel W.
  • 370
  • 1
  • 10
1
vote
1 answer

UIImageView does not update in a thread made by NSURLConnection?

I do a connection to server using Async connection this way: // Create the request. [indicator startAnimating]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString …
MBH
  • 16,271
  • 19
  • 99
  • 149
1
vote
2 answers

How to store a block definition that returns something, in a local variable

This seems a little strange to me that when my block does not returns anything, I can capture it in a variable before passing it to a consumer metod. But as soon I add a return value to the block typedef, I start getting waring Scenario 1: Block…
geekay
  • 1,655
  • 22
  • 31
1
vote
1 answer

iPhone 5 to iPhone 4 screen size

I have made an app for the 4-inch iPhone and was wondering how I can make it also run on a 3.5 inch screen without it just cutting off part of the screen. I have read about auto layout but I'm not sure if my project is set up for it. My view…
1
vote
2 answers

Return NSArray in block

How can I return the array that is being filled in the completion block? It is some JSON being parsed into an array of objects. -(NSArray *)GetMapVehiclePoints { NSString *methodURL = [NSString stringWithFormat:@"%@GetMapVehiclePoints",…
Clip
  • 3,018
  • 8
  • 42
  • 77
1
vote
2 answers

Passing blocks with unknown signatures as parameters in objective c

I'm making a graph, and I want to make a system for specifying the conditions to transition between nodes along an edge. I am unsure of what the method signature should look like, since different transitions will have different parameters. So the 2…
rurulu
  • 42
  • 5
1
vote
1 answer

UIKeyboardTaskQueue threading issue

I'm fairly new to iOS development and I've been stuck on this bug for a while. I'm making a simple app the uses a web service. Right now I currently have two view controllers. A login view controller (with its NIB file) and a main view controller…
user2604504
  • 697
  • 2
  • 14
  • 29
1
vote
2 answers

Why can setting a Block variable inside an if statement cause a carsh?

I found an block example in the book "Effective Objective-C 2.0" void (^block)(); if (/* some condition */) { block = ^ { NSLog(@"Block A"); }; } else { block = ^ { NSLog(@"Block B"); }; } block(); The code is…
lingguang1997
  • 451
  • 1
  • 4
  • 12
1
vote
1 answer

setting BOOL variable inside block

I'm trying to set a BOOL value which is defined in my class inside a block, but I can't see to be able to set it. This is the code. __weak __block SPTween *tween2weak = tween; __block BOOL buttonScroll2 = buttonScroll; tween.onComplete =…
Phil
  • 2,995
  • 6
  • 40
  • 67
1
vote
1 answer

Should block handlers be nil out when completed?

Should all passed block handlers be nil out when the class is done running? What happen if none of the blocks are nil'ed at all? For example, the following code: - (void)runWithCompletionHandler:(void (^)(id results))completion …
Boon
  • 40,656
  • 60
  • 209
  • 315
1
vote
2 answers

Factory methods in Objective-C

I make class factories like so, @implementation Universe { NSString *foo; } + (instancetype)universeWithMeaning:(NSString *)meaning { return [[self alloc] initUniverseWithMeaning:meaning]; } - (id)initUniverseWithMeaning:(NSString…
paulvs
  • 11,963
  • 3
  • 41
  • 66
1
vote
1 answer

Does compiler saves argument of a function?

I have a function which is loading image by index in another thread using GCD . So lets assume this : -(void)loadMainImageToIndex:(long)index { NSDictionary *dic=[mainData objectAtIndex:index]; NSString *userImageUrl=[dic…
Curnelious
  • 1
  • 16
  • 76
  • 150
1
vote
2 answers

Why the action triggered by a NSTimer can't be specified by a block?

When creating a timer, there are only these options: + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats; + (NSTimer…
Lay González
  • 2,901
  • 21
  • 41
1
vote
1 answer

UIView Animation doesn't run in correct order in for loop - iOS

I have a simple for loop which animates some UILabels. The reason I have the animation block code in my for loop is because obviously I have more than one label I am trying to animate. Now my program increments the numbers in the UILabels by one…
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
1
vote
1 answer

Using block with ternary operator in Objective-C

Is there a way to use blocks with the ternary operator in Objective-C? I'm trying to do something like: [self evaluate] ? ^{ // do somethings } : ^{ // do something else }
Oscar Swanros
  • 19,767
  • 5
  • 32
  • 48