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

Iterating through blocks vs fast enumeration vs for loop

I thought iterating through Blocks is faster than enumeration and in some case it does. However with this simple example where I have a data array and I am creating multiple arrays using different iterating approaches and the results are not…
1
vote
2 answers

Grand Central Dispatch, dispatch queue: enumerate array and create many concurrent tasks for each element

So, I have array(fetch result from Core Data). I want iterate it, and for each element of the array (url, and other data) create a new parallel task (network). Only after completing all these tasks need to start a new parallel task. Tell me how to…
Vladislav
  • 165
  • 1
  • 5
  • 15
1
vote
1 answer

dispatch_async with c++ instance - app crashes

I want to use Tesseract in my Mac App, but recognizing text blocks the complete application. Therefore I tried to use a dispatch_async, but my app crashes with a EXC_BAD_ACCESS (at "tess->Recognize(0)". Here is the method, I'm using: -…
Lupurus
  • 3,618
  • 2
  • 29
  • 59
1
vote
0 answers

NavigationController does not push inside the completion block

I am working on a piece of code where when the users presses the submit button, he is asked to update his/her password before he is able to go to the app screen. SO when you press the button, the 'Login' procedure gets executed, and the user is…
jerry
  • 274
  • 2
  • 19
1
vote
1 answer

ALAssetLibraryGroup find camera roll in block

I am trying to get the ALAssetsGroup "camera roll" when self.assetsGroup is nil. My problem is that it is async, and I am trying to figure out how I could do this sync...if its possible. - (void)viewDidLoad { [super viewDidLoad]; if…
slik
  • 5,001
  • 6
  • 34
  • 40
1
vote
1 answer

Wait until all iOS blocks are executed before moving on

I have a data model/store object that interfaces with the Internet over several APIs containing data. The number of APIs to be interfaced with is dynamic: from a conceptual standpoint, we can consider the endpoints to be strings in an…
Uzumaki Naruto
  • 547
  • 5
  • 18
1
vote
2 answers

Objective-C - Is there a way for an Object to execute a method IMP directly as if it were its own?

Presume I have an Object, an instance of MyClass. In Objective-C one can ask the Object to "perform" a selector by either sending it a message or using NSObject's "perform". This selector has to be defined at compile time as part of the Class…
unom
  • 11,438
  • 4
  • 34
  • 54
1
vote
3 answers

Getting variable from the inside of block

I'm using AFNetworking 2 to GET data from server and I need to get back the responseObject but no matter what I do i still get . Here is the method which is sending GET request to server and in response it gets NSDictionary which I want to use…
cojoj
  • 6,405
  • 4
  • 30
  • 52
1
vote
0 answers

Code inside SKAction completion block not working as expected

I've implemented a "pause menu" in my game. When the user taps the pause button on the top left corner of the screen, I run the following code: - (void) pauseGame { // Blur the game's contents: [_effectNode setShouldEnableEffects:YES]; …
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
1
vote
3 answers

How do I return a value determined within a delay_after block?

Very new to Objective-C and having a rough time figuring out how to accomplish the following. I come from a javascript background, so I may not be approaching this in the correct way. In my view controller I'm making a call to a class method…
Rick
  • 61
  • 1
  • 4
1
vote
1 answer

how to use badges on uibutton like message app?

I am newbie for iPhone development.i just create application like chat application.simple i use JSON parsing method and i call the data from server through PHP and send back to them. so i have one UIButton i would like to set badge on this UIButton…
kins
  • 53
  • 1
  • 5
1
vote
1 answer

How to schedule an asynchronous task every 'x' seconds when main and background thread need same data?

I have an iOS application that has an NSTimer which fires every 5 seconds. This then posts a notification, telling several controllers that they must now recalculate some data and then update their UI. When this all happens on the main thread,…
1
vote
1 answer

Is it possible to create methods and variables, set them at runtime in Objective-C?

For example: @implementation MyClass{ NSNumber *something; } I would like to be able to add a few other like it at runtime to a particular object (just one instance), not the entire class. They already have pointers to other objects in them and…
1
vote
1 answer

Why is Objective-C block syntax so different from method syntax?

The question says it all. Why: (void)methodWithParamA:(id)paramA paramB:(id)paramB; [obj methodWithParamA:valA paramB:valB]; and: void(^ block)(id paramA, id paramB); block(valA, valB); I'm not sure I'll necessarily gain anything by learning the…
ericsoco
  • 24,913
  • 29
  • 97
  • 127
1
vote
1 answer

Get animation duration in a custom class

I'm trying to create an animation in a custom class that will be triggered in an animation block like all UIView elements. my class: @interface SmileyFace : NSObject @property (strong, nonatomic) uicolor* color; @end I'm changing color…