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

A block fail importing a nested block

I have this code in a method: - (NetworkOperation *)runOperationWithPath:(NSString *)path params:(NSDictionary *)params httpMethod:(NSString *)httpMethod …
Luca Bartoletti
  • 2,435
  • 1
  • 24
  • 46
1
vote
2 answers

Cannot AddObject to NSMutableArray from Block

I have a feeling that my problem here is really with blocking, but maybe it's something else too. I am trying to forward geocode an address and place the coordinates into an array to use later. An exception is raised down at the bottom when I try to…
TedCap
  • 185
  • 1
  • 1
  • 9
1
vote
1 answer

__weak seemingly retains the object

Objective-C weak properties should point to nil if the object gets deallocated, but in this case weak properties seem to retain the object. Consider the case: @interface SillyObject : NSObject @property (nonatomic, assign) NSInteger…
SiimKallas
  • 934
  • 11
  • 23
1
vote
1 answer

How to disable modal segue iOS

I have an app having 7 screen. On the screen 7 i have a button that does valiation and submits data and then it jumps to screen 1 using modal segue. But ,I only want to move screen1 if the validation succeeds or else i dont want to move to…
Lalit_vicky
  • 295
  • 1
  • 5
  • 15
1
vote
1 answer

Need assistance understanding __block Storage Type

__block NSString *x = @"123"; // x lives in block storage void (^printXAndY)(NSString*) = ^(NSString *y) { x = [x stringByAppendingString:y]; printf("%@ %@\n", x, y); }; printXAndY(@"456"); Apple docs says: The __block Storage Type You…
S.J
  • 3,063
  • 3
  • 33
  • 66
1
vote
2 answers

No matching function for call to... but only inside a block?

I've got a strange situation. I have some local variables in a function: JSContext *cx = ...; jsval successCb = ...; There is a function call which takes these parameters: //JS_RemoveValueRoot(JSContext *cx, jsval *vp); JS_RemoveValueRoot(cx,…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1
vote
1 answer

Is there a modernized way of getting a weak self reference for blocks?

I see old sample code use this code a lot: __unsafe_unretained __block AssetItem *weakSelf = (AssetItem *)self; I remember that something has changed and this became easier. So is there now a modernized way of doing this?
1
vote
3 answers

Why the retain cycle warning not thrown?

I use Xcode 5 and have some code @interface Controller { __weak IBOutlet UIView *someView; } @implementation Controller { - (void)doSomething { [UIView animateWithDuration:0.5 animations:^{ someView.hidden = YES; }]; } -…
efpies
  • 3,625
  • 6
  • 34
  • 45
1
vote
0 answers

Icons won't display in viewDidLoad or ViewDidAppear outside of block?

For some reason anytime I try to add annotations they don't show up automatically. I've pinpointed the issue. It's because anything outside of this block won't be executed in my ViewDidLoad. How would I make it so once this method has been run it…
Lalalalalala
  • 167
  • 1
  • 1
  • 10
1
vote
1 answer

How to update CollectionView's numberOfItemsInSection after block finishes executing

I would like to put up a UICollectionView of pictures downloaded from a backend provider, and am running the issue where every time my collection view controller is initialized, the required method - (NSInteger) collectionView:(UICollectionView…
daspianist
  • 5,336
  • 8
  • 50
  • 94
1
vote
1 answer

Using Stackmob getLoggedInUserOnSuccess:^(NSDictionary *result) onFailure:^(Error *error) method

When I implement the getLoggedInUserOnSuccess:onFailure method (or the loginWithUsername: password: onSuccess:^(NSDictionary *results)...method in xcode, the results array does not become available until after all of my code has finished running.…
1
vote
1 answer

What would happen if I dispatch_barrier_(a)sync-ed to a queue that targets a global concurrent queue in GCD?

I have a question about dispatch_barrier and the target queue. I have a custom serial queue and custom concurrent queue and I set the target queue of the serial queue to a concurrent queue which then targets a global concurrent queue: (serial…
1
vote
1 answer

_objc_empty_vtable The file "..." couldn't be opened because there is no such file

this is a self answering question. Putting it out there to help others. I just had a really interesting crash with my app. What I wanted to do was to define a sort comparator that would be returned a static method in the model. So, for example if my…
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
1
vote
2 answers

How to keep a strong reference inside of a block without having a retain cycle

I have a block attached to a button (using this category): __unsafe_unretained typeof(UIImage) *weakPic = originalPic; [button addEventHandler:^{ switch (state) { case state1: { UIViewController *vc = //some VC …
abbood
  • 23,101
  • 16
  • 132
  • 246
1
vote
1 answer

Strategies for tracking down _objc_msgSend crashes inside dispatch_sync block

I'm receiving crash reports (via the excellent Hockey) indicating that I've got a memory problem in some code that's called inside a dispatch_sync block (or at least that's how I'm interpreting the crash report snippet below). I've not been able to…