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

Why is block designed to be allocated on the stack unless copied?

Why has Apple designed block to be allocated on the stack unless copied? What's the benefit for such behavior? Why not just make it behave like a regular NSObject - alloc-init it and it goes on the heap automatically?
Boon
  • 40,656
  • 60
  • 209
  • 315
1
vote
1 answer

How should you use release with a __block variable?

The code below works fine, I just don't know where the release should go, because I'm not sure what the rules are. I'm not using ARC. - (void)myFunc { // stuff happens __block UIImage* photo = [UIImage imageWithCGImage:croppedCGImage]; …
Curyous
  • 8,716
  • 15
  • 58
  • 83
1
vote
2 answers

Can't retain block retrieved from within another block

I am trying to write a unit test for a method which itself creates and passes a block to another object (so it can be called later). This method is using socket.io-objc to send a request to a server. It passes a callback block to socketio's…
1
vote
2 answers

Block passing itself as argument to method causing compiler warning

I was trying to create a callback that retries the method after a delay on failure. I'm hitting this warning: "Capturing failure block strongly in this block is likely to lead to a retain cycle." typedef void (^MyCallbackBlock)(NSObject…
Ben Flynn
  • 18,524
  • 20
  • 97
  • 142
1
vote
0 answers

Obj-C: Issue with object from block

Im pretty new in programming and im currently looking at blocks which im having some issues with. Im having a hard time reaching an object which is defined within my block. Actually I cannot really explain this issue in an accurate way, so ill give…
mar-schmidt
  • 394
  • 4
  • 11
1
vote
1 answer

Calling UIAlertView in block displays view multiple times

I have a block completion being called from within a button press message and, depending on state, optionally a UIAlertView being displayed. However, when invoked the UIAlertView appears three (3) times... With the full information but it…
Frank C.
  • 7,758
  • 4
  • 35
  • 45
1
vote
1 answer

Error code 512 while calling setUbiquitous:

I know this question has been asked before but so far none of the posts that I found solve my problem. Here's my codes NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent: @"Documents"]…
Cadrick Loh
  • 721
  • 1
  • 7
  • 19
1
vote
2 answers

ios: Updating UITableView inside AFNetworking success callback

I'm trying to load data from API and show it inside a UITableView. I used AFNetworking for the network calls, but now I'm facing a problem: I can't access myTableView nor self inside the success block. @property (weak, nonatomic) IBOutlet…
trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
1
vote
2 answers

Modify global variable in Block iOS

I have blocks that loads data from a server, the problem is that I can not affect my result in a global variable in the block [URLImages asyncRequest:RequestForPopular success:^(NSData *data, NSURLResponse *response) { …
1
vote
1 answer

UIView animation stop when UIView willDisappear

I am not much into iOS animation. What I am trying to achieve is a simple message view that slide vertical from bottom of screen to a given y, then after few instants the UIView rollback in vertical to go off screen. [UIView…
Leonardo
  • 9,607
  • 17
  • 49
  • 89
1
vote
2 answers

Mapkit :How to add annotation on MKLocalSearch results placemarks?

Basically I need a way to put annotation on all the "Walmarts" that search request picks up. I am not using interface builder, I am just using code for this app. MKMapView * map = [[MKMapView alloc] initWithFrame: CGRectMake(0,…
moomoo
  • 77
  • 7
1
vote
5 answers

What is the use of storing the block in an instance variable

I am aware that blocks are one of the latest feature added in ios. But I am really finding a tough time learning it . I have seen people doing the following typedef void(^CallBackBlk) (NSString *); @property(copy,nonatomic)CallBackBlk block; and…
Raj
  • 1,119
  • 1
  • 15
  • 32
1
vote
3 answers

Animating UITableViewCells one by one

I am trying animation, where the UITableView is already in place,but the subview (which itself contains the cell's content view) for each of the UITableViewCells is outside the window bounds. It is outside the screen. When the view loads, the cell…
1
vote
1 answer

DIsplay 'data loading' while using sendAsynchronousRequest:queue:completionHandler:

I am using this block method to load my data from the server... NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]] …
sangony
  • 11,636
  • 4
  • 39
  • 55
1
vote
1 answer

Does using blocks auto create new threads?

JUST started doing work with blocks... very confusing. I am using a block like this: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *myDictionary = [[mySingleton arrayPeopleAroundMe]…
sangony
  • 11,636
  • 4
  • 39
  • 55
1 2 3
99
100