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

Enumerate/Iterate over an array using Blocks and know when it is completed

I'd like to iterate through an array in Swift and I need to know when the last item has been reached. I am using enumerateObjectsUsingBlock. As there is no optional completion block (like in some of the CoreAnimation methods) I assume the stop…
Bernd
  • 11,133
  • 11
  • 65
  • 98
1
vote
2 answers

Idiomatic way to execute an array of blocks

I have an object that can execute an arbitrary queue of updates. I use blocks to embody the updates. I add an update using my addUpdate: method. - (void) addUpdate: (void(^)())block { [self.updates addObject: block]; } Later, I want to execute…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
1
vote
0 answers

iOS Method with blocks in container class

I am looking for some solution that could help me reuse methods that use Restkit. I have these methods in my controller: RKObjectManager *restManager = [RKObjectManager sharedManager]; [restManager getObjectsAtPath:@"somePath" …
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
1
vote
1 answer

GCD dispatch_set_target_queue function's 1st parameter type

The function prototype is this: void dispatch_set_target_queue( dispatch_object_t object, dispatch_queue_t queue); typedef union { struct dispatch_object_s *_do; struct dispatch_continuation_s *_dc; struct dispatch_queue_s *_dq; …
J-Q
  • 374
  • 5
  • 15
1
vote
3 answers

iOS return block value from method

How I can return variable "myDoub(for example=65)" out of method and out block ?? - (double)executeRequestUrlString:(NSString *)urlString withBlock:(double (^)(double myDoub))block { [NSURLConnection sendAsynchronousRequest:request …
Dm Kuz
  • 68
  • 1
  • 6
1
vote
2 answers

How to pass data from form FormSeetController to the ViewController?

i trying to use this package: https://github.com/m1entus/MZFormSheetController now my question, i trying to pass data between the FormSeetController to the ViewController after i dismiss the formSheet. here what i tried so far: in the ViewController…
1
vote
1 answer

Nesting blocks in iOS

I'm trying to be able to call a 'completionHandler' block from inside another completionHandler block (called after an asynchronous URL request). This however results in the application crashing with the following message (I'm using Zombie…
ABC
  • 718
  • 8
  • 23
1
vote
2 answers

Swift - AnyObject[] is not a subtype of AnyObject[]?

I'm trying to call an objective C method that takes a block that calls back with an NSArray. I get the following compile error: AnyObject[] is not a subtype of AnyObject[] [query fetchPlaces:^(NSArray *places, NSError *error)…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
1
vote
1 answer

Does the block declared inside a @synchronized section get synchronized?

While implementing lazy thread-safe getter I encountered an interesting (in my opinion) situation where I had to use @synchronized section with block declared and invoking inside this section. Here is the simplified version of the code: - (void)…
nalexn
  • 10,615
  • 6
  • 44
  • 48
1
vote
1 answer

Multiple blocks in each other, using strong self and weak self to avoid retain cycles

I did get used to write __weak typeof( self ) wself = self [smth doSomeBlock:^(void) { __strong typeof( wself ) sself = wself; [sself callAny]; }] So i avoid retain cycles But what to do when multiple blocks are nested? __weak typeof( self )…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
1
vote
0 answers

Run task after first compete

How I can run third method after playing will finish ? Playing audio in other stream I have some method - (void)play { [self runTask:^{ [self someMethod]; } withCompletion:^{ [self secondMethod]; }] ;} -…
1
vote
2 answers

UIImage don't release memory

I have a strange problem with memory management which makes me crazy. I need to preload images asynchronously. I have a code - (void)preloadFinishAnimation { self.animationImages = [NSMutableArray new]; __weak LearningViewController…
1
vote
2 answers

How to assign a variable inside a block to a variable outside a block in objective-c

Refer to Assign a variable inside a Block to a variable outside a Block It says it should add __block to the variable, but I see some codes in Google Youtube sample code for objc here…
bandw
  • 879
  • 2
  • 11
  • 30
1
vote
1 answer

Data is getting inserted in unordered manner in iOS

I am trying to insert sample data into core data by fetching it from plist. But that data is not getting inserted in Ordered manner. Below is my plist structure. Below is the code which I tried. NSArray *aArray = [NSArray…
iLearner
  • 1,670
  • 2
  • 19
  • 45
1
vote
2 answers

Trouble using subclasses of PFObject in blocks such as fetchIfNeededInBackgroundWithBlock

I'm having trouble understanding how to use subclassed objects with blocks. Here is an example of what I'm trying. PFItem is a subclass of PFObject. - (void) handleItem:(PFItem *)item{ [item fetchIfNeededInBackgroundWithBlock:^(PFItem *item,…