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

Return value from inside block (Objective-C)

I've been trying to get a value from inside a block for a few hours now, I can't understand how to use the handlers on completion and literally everything. Here's my code: + (void)downloadUserID:(void(^)(NSString *result))handler { //Now…
AndrewSB
  • 951
  • 3
  • 11
  • 25
1
vote
4 answers

ALAssetsLibrary, wait until assetForURL is finished

I have an array with imageurls that corresponds to images in the assetslibrary, and I need to fetch all of them before I do a certain task. Whats the best way of doing this? Should i use the NSNotificationCenteror would it better to use blocks, if…
Odd
  • 95
  • 6
1
vote
3 answers

memory growth when using blocks & Path's FastImageCache

I am using Path's excellent FastImageCache library and am running into a retain problem. Specifically, when trying to provide UIImages (downloaded from a server) to the image cache. The images, when handed to the completion block, seems to remain…
1
vote
2 answers

Why need to copy the objective-c handler before cast it to void* type?

I want to simulate the NSAlert member function which only exist on 10.9. - (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9); The code is as…
ZijingWu
  • 3,350
  • 3
  • 25
  • 40
1
vote
0 answers

Objective-C block enumeration design pattern

Many classes have enumeration functions like this one for NSArray: - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block What is the correct way (design pattern) to implement this in your own class? The code below…
user965972
  • 2,489
  • 2
  • 23
  • 39
1
vote
1 answer

Need to get result of "For Loop" with nested Async call (Objective C)

I need to get the final activeTime value once the loop has completed. The code below runs a loop, which in turn calls a block many times (which runs an async process). Each loop will increment activeTime if more than 10 steps occurred in that…
1
vote
1 answer

Changing @property value of self while executing a block that uses a strong reference to self

I'm working on doing network requests in my app and am using NSBlockOperations in an NSOperationQueue to do this asynchronously. However, I want to be able to cancel these operations if the view controller that called them is deallocated (has been…
1
vote
1 answer

Add Subview to ViewController with Block and ARC in encapsulated Class

i'am searching for a way to add a Subview (in my example a UIPickerView) to a ViewController like MBProgressHUD. My Subview is an UIView which has a UIPickerView and a UIButton on it to select an item. I use this view in different ViewControllers,…
1
vote
4 answers

incompatible blocks type assigning

I have used blocks a few times but never with declaring a typedef first. I am trying to setup this definitions on a class (lets call it ClassA) typedef void (^myBlock)(); @property (nonatomic, copy) myBlock onCompletion; Then I create an instance…
Duck
  • 34,902
  • 47
  • 248
  • 470
1
vote
1 answer

iOS having issue setting a property in afnetworking 2.0 success and failure block

I am trying to build an ios app for our website. We have implemented api using oauth 2.0. Now, I am having an issue setting a property. I am using afnetworking2.0 for networking. Here is what I am trying to achieve: I am writing functions that will…
1
vote
2 answers

UIBarButtonItem image not set

I have very strange problem. I'm initializing some UI elements inside completion block of AFNetworking, and only one of them (UIBarButtonItem) is not initializing. Here is my code which is placed in viewDidLoad: AFHTTPRequestOperation *operation =…
1
vote
1 answer

How to get the name of block?

E.g, we have typedef id(^func)(id); func read_file = ^(NSString *path_to_file) { return [NSString stringWithContentsOfFile:path_to_file encoding:NSUTF8StringEncoding error:NULL]; }; I wonder how can we get the name of this block if I passed…
Kun Hu
  • 417
  • 5
  • 11
1
vote
1 answer

Objective C - OCMock expect a method that has a block argument?

I have a method that takes a string and a completion block argument. I only care about the string argument, but OCMockObject throws an exception, what should I pass as the block argument? My Protocol @protocol SomeService -…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
1
vote
2 answers

SIGSEGV when setting block to nil

I'm running into a strange crash that's difficult to reproduce. For instance, out of the past 35 runs of the app, this crash happened once. I'm not sure of the exact steps to repro unfortunately. The crash report shows: SIGSEGV Remotely-[NetworkCall…
adam.wulf
  • 2,149
  • 20
  • 27
1
vote
1 answer

Objective-C Blocks Error

I just start learning Objective-C. I got an error when trying to make very small Objective-c Block example. It always shows "expected identifier or '(' before '^' token" errors?. COULD YOU PLEASE TELL ME WHERE DID I MAKE WRONG? #import…
Toan Tran Van
  • 695
  • 1
  • 7
  • 25