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

UIButton rotation animation not rotating back with CGAffineTransformMakeRotation

I have a button (openMenu) that I am animating down (works fine) and rotate (works the first time) with a "drawer". The first time it rotates it works.. but when I try to rotate again it wigs out and hides the image and shows the button title? Any…
1
vote
1 answer

Access Stack Block from within Block using ARC in Objective-C

My question regards blocks in Objective-C: Assume the following situation (with ARC enabled): typedef NSString*(^MyBlockType)(); typedef NSString*(^MyReturnBlockType)(MyBlockType); - (MyReturnBlockType) foo: (MyBlockType) block { return…
1
vote
1 answer

Does calling executions on dispatch_queue_t ensure they are on the same thread?

If I store a dispatch_queue_t like so: @property(assign, nonatomic) dispatch_queue_t myQueue; ... _myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); Later, when I do operations like dispatch_async(_myQueue, ^{ …
1
vote
1 answer

iOS block release and self retain cycle

Im doing some research on blocks, the code here typedef NSString* (^MyBlock)(void); @property(copy,nonatomic) MyBlock block1; in viewdidload self.block1 = ^{ self NSLog(@"do block"); return @"a"; }; of course the self is retained,…
nickyu
  • 141
  • 1
  • 11
1
vote
1 answer

addObserverForName and removing observer

On Cocoa code with ARC enabled, I tried to observe Window closing events like below. ScanWindowController * c = [[ScanWindowController alloc] initWithWindowNibName:@"ScanWindowController"]; [scanWindowControllers addObject:c]; [c…
1
vote
1 answer

How to avoid nested blocks

GCD and blocks are so nice and convenient. But when I fall in love with it, I found that something bad happened. Look at these codes below: [self functionA:^(BOOL success) { if (success) { …
1
vote
2 answers

NSComparator block not called

I am fetching data from Core Data storage using an NSFetchRequest and store that data in an array - all works great. As a next step, I want to sort that array using NSSortDescriptors like so: array = [array sortedArrayUsingDescriptors:[NSArray…
artooras
  • 6,315
  • 9
  • 45
  • 78
1
vote
0 answers

Calling a Completion Block inside of a Completion Block results in Bad Access

Solution: Turns out the problem doesn't exist in this code. It's because the foo is being called in a UIViewController when it's being destroyed. Thus, when the asynchronous foo calls the completion block, it breaks due to a call to something that…
DrDisc
  • 281
  • 4
  • 12
1
vote
1 answer

ios: self becomes nil in block?

I have a view controller, which calls several async network ops (I am using AFNetworking), and handles the response using blocks. Consistently - one of the blocks shows me that self == nil, but other blocks in the SAME view controller, using the…
Sagi Mann
  • 2,967
  • 6
  • 39
  • 72
1
vote
1 answer

Why does this method pass 'stop' by reference instead of return?

Does anyone have some insight into why the block parameter of - (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range …
griotspeak
  • 13,022
  • 13
  • 43
  • 54
1
vote
1 answer

Nested UIView animateWithDuration:delay:options:animate:completion not delaying

// Animate moving the cards into position [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations: ^{ card.frame = cardFrame; } …
1
vote
1 answer

Is there any convention that indicates when a block executes in synch with the current thread?

In Objective-C blocks can be run asynchronously or synchronously, depending on the purpose. Just looking at an API method won't tell us which way will happen. It would be nice if there was a convention that indicates whether a block is going to be…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
1
vote
1 answer

fast enumeration versus enumerateObjectsUsingBlock

Consider: - (void) testing { NSMutableArray * numbers = [NSMutableArray arrayWithCapacity:10000000] ; for (int i = 0 ; i < 10000000 ; ++i) { [numbers addObject:@(i)] ; } NSInteger (^sum)(NSInteger, NSInteger) = ^(NSInteger…
verec
  • 5,224
  • 5
  • 33
  • 40
1
vote
1 answer

FBRequestConnection startForCustomAudienceThirdPartyID:nil works on simulator, not on device

Using FB SDK 3.6, I am attempting to capture FB User IDs and save to Parse datastore in the cloud to build a custom audience to market to. My call is as follows: [FBRequestConnection startForCustomAudienceThirdPartyID:nil …
1
vote
1 answer

obj-c weak self in a block: why the 2nd one doesn't need a weak self inside in two similar cases

I finally found my memory bug is caused by referring self strongly in a block. But I don't know why in a similar case, the weak is not needed: I have a CameraCaptureManager class doing image capture tasks, and a CameraViewController has a strong…
X.Y.
  • 13,726
  • 10
  • 50
  • 63