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

Multicast Block: How to generalize

Goal I have a class with various properties that can be used to plug in a block to receive certain events. @interface SomeClass @property (copy, nonatomic) void (^handler)(int arg1, int arg2); @end In the client code, I would like to dynamically…
Etan
  • 17,014
  • 17
  • 89
  • 148
1
vote
2 answers

iOS -- accessing local vars inside blocks

I have a bit of code as follows: companyLogo = nil; [DLImageLoader loadImageFromURL:image_url completed:^(NSError *error, NSData *imgData) { if (error == nil) { __block…
stackOverFlew
  • 1,479
  • 2
  • 31
  • 58
1
vote
3 answers

blocking in php like objective c blocks?

I am working with a PHP project where I rename files, sometimes it takes time for these files to rename. I want to detect when they have finished renaming. I know in Objective C there are blocks that will perform a task and on completion give the…
user906357
  • 4,575
  • 7
  • 28
  • 38
1
vote
1 answer

I'm struggling to implement a subclass of UIDynamicBehavior

I'm trying to move a UIView across the screen. UIGravityBehavior and UIPushBehavior both take things like density, velocity and friction into account. I tried to implement my own dynamic behavior that ignores those physics. My UIDynamicBehavior…
1
vote
4 answers

What is the correct way to be sure that the object will be there and it won't leak while using blocks with ARC on iOS?

Which of the following code section is correct? Definition of correct for "me": It should not have a retain cycle and so should not leak. It must guarantee the running of method2 and method3. So MyObject variable in block must never ever be…
1
vote
2 answers

Best way to create nested callbacks in ObjectiveC

I have a class that handles interaction with an external API, and I'm trying to figure out the best way to break them into descreat chunks. At the moment, it looks something like this: - (BOOL) testCredentialsWithUsername:(NSString *)username…
Josh Hunt
  • 14,225
  • 26
  • 79
  • 98
1
vote
1 answer

Objective C: Use Blocks to Notify When Array is Populated

I'm new to using blocks, and they really seem like a great alternative to delegate methods. I implemented a simple block to do some simple math after watching a few tutorials, but I'm really struggling in being able to get them to do much more than…
William LeGate
  • 540
  • 7
  • 18
1
vote
2 answers

How to make particular part of function wait in iOS

I'm trying to do a share operation where I call a function with async block but in my next if statement I need to get the value which is completed in the block to continue. This is my code which will highlight more detail. I heard about NSLock and…
Francis F
  • 3,157
  • 3
  • 41
  • 79
1
vote
1 answer

Can't retrieve the objects from a JSON steam to enumerate

I'm trying to get a grasp on how to use block in Objective C. In this simple app I'm writing, I'm getting a set of images as a JSON stream. I'm trying to get the images out and objectify them and then collect them into a NSMutableArray. Below is my…
Isuru
  • 30,617
  • 60
  • 187
  • 303
1
vote
1 answer

In iOS, using NSOperation with nested completion block results in repeated EXC_BAD_ACCESS

I'm trying to fetch remote web images using NSOperation and completion blocks. Essentially, the receiving object (view controller) will call the SGImageManager's fetchImageWithUrlString:completionBlock method, which in turn will setup an…
abc123
  • 8,043
  • 7
  • 49
  • 80
1
vote
1 answer

Cancel objective c block in NSObject iOS

I am working on an iOS app that uses navigation controllers. In several of the view controllers I create an instance of a class, Request. In this class I have a method that has a block: - (void)submitRequest:(NSMutableDictionary *)dictionary { …
user906357
  • 4,575
  • 7
  • 28
  • 38
1
vote
1 answer

Is enumerateUsingBlock: thread safe?

Is enumerateUsingBlock: thread safe? I mean, can I mutate an NSMutableArray while enumerating it using enumerateUsingBlock on another thread? How about enumerateObjectsWithOptions:usingBlock: when using NSEnumerationConcurrent option?
Mirek
  • 470
  • 4
  • 18
1
vote
0 answers

MagicalRecord never gives success in completion block

Magical Record 2.2: [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { NSArray *cats = [Categorization MR_importFromArray:response.result]; }completion:^(BOOL success, NSError *error) { …
jimijon
  • 2,046
  • 1
  • 20
  • 39
1
vote
3 answers

Calling methods inside block and trying to reload data crash

I use parse.com as backend, and my query was very slow, so I am trying to change it to use blocks. Basically, my query populates an array with everything I need, and according to if statements, I'm calling methods inside the block, these methods…
Jorge
  • 1,492
  • 1
  • 18
  • 33
1
vote
3 answers

iOS: method returning wrong value of a boolean variable set inside a block

I have a method which returns a boolean value. It should return true if at least one image/asset has been found from a URL. The code is as shown below. Inside the block when i print the count of objects in the array, it prints properly. However,…
rkk817
  • 117
  • 1
  • 13