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

Perform upload after user selects Activity from UIActivityViewController - Possibly an issue with blocks

I want to perform an upload task once a user has selected a share activity from UIActivityViewController, but before the share sheet is shown. Specifically, I need the url of the uploaded image to use in the Activity. I already have subclassed…
Darren
  • 10,182
  • 20
  • 95
  • 162
1
vote
1 answer

Objective c blocks executing and reusing

I'm trying to understand how objective c blocks work. As far as I understood: block is just a function with no name. So for example: ^(int a, int b) { return a + b; }; How can I invoke it? Can I use it multiple times as I would use a function?…
1
vote
2 answers

Unexplained `capturing self strongly` warning

I have a number of methods in blocks, firing one inside the next, in order to sync some data with a web service. Most of these behave completely fine, but one method won't let me mention self after it's been called, giving me a capturing self…
Andrew
  • 15,935
  • 28
  • 121
  • 203
1
vote
1 answer

Setting object property inside a block trouble on Objective-C

I'm starting to learn Objective-C for iOS Development, and a got a issue that is driving me crazy. All that I want is to do a request, retrieve e JSON and then set this JSON into an instance property. -(NSArray *) retrieveAtEndpoint:(NSString…
Diego Charles
  • 103
  • 12
1
vote
1 answer

Kiwi - Expected subject to be nil, got KWAsyncVerifier

I'm using Kiwi to write the tests for my app. I wrote tests to test against my API. I was guided by this example in the documentation for testing asynchronous calls: https://github.com/allending/Kiwi/wiki/Asynchronous-Testing My tests are long, so…
Kex
  • 776
  • 10
  • 22
1
vote
2 answers

animation options not respected/error in second animation block

Using a simple CGAffineTransformMakeScale to make a pulsing label, I get an error when trying apply ease out options, delay etc on the second block? Without this I get jerky animation as im getting ease out on the increase and none on the return to…
JSA986
  • 5,870
  • 9
  • 45
  • 91
1
vote
2 answers

Is it possible to have a __block variable point to different objects, without altering the blocks copied before?

I've asked a similar question before, but this time the situation is a bit different. For one, this time around I'm using ARC (inviting a whole new world of problems). Second: The code I have basically works, but I'm wondering if I could do with…
ATaylor
  • 2,598
  • 2
  • 17
  • 25
1
vote
1 answer

Does block parameters need to specify ownership qualifiers when providing weak references?

I'm trying to use blocks in a way where I provide a reference to the object which retains the block, as follows: typedef void(^RunBlock)(__weak Thing *block_owner, ThingFinishBlock finish); where Thing has a property run_block, of the type…
Goos
  • 121
  • 6
1
vote
1 answer

Is it safe to add to a collection (mutable NSArray/NSDictionary) inside enumerateObjectWithBlock?

Is this code safe to execute? That is, is it safe to add to a NSMutableArray or NSMutableDictionary concurrently? - (NSArray *)batchProcess:(NSArray *)inputList { NSMutableArray *outputList = [NSMutableArray arrayWithCapacity:inputList.count]; …
1
vote
1 answer

What am I doing wrong with this method definition and call?

I have this in my class header: typedef void(^DBSuccessBlock)(); typedef void(^DBErrorBlock)(int errorNumber, NSString* description); - (void) connect:(NSString*) path isFile:(BOOL) flag success:(DBSuccessBlock) success …
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
1
vote
1 answer

Detecting if a method is being called from a block

I'm using AFNetworking's AFHTTPClient class to communicate with a Rails backend. When creating objects, I want to run several API calls on the server using a batch API (I'm using batch_api in case you were wondering). To nicely extend AFHTTPClient I…
pgb
  • 24,813
  • 12
  • 83
  • 113
1
vote
0 answers

lost CTCallStateConnected when use CTCallCenter

I used following codes to get call's status: CTCallCenter *callCenter = [[CTCallCenter alloc] init]; callCenter.callEventHandler = ^(CTCall* call) { if (call.callState == CTCallStateDisconnected) { NSLog(@"Call…
Spence
  • 11
  • 2
1
vote
1 answer

Setter not called when call weakSelf in block

When we need to call self in a block, we usually do as this: __typeof(&*self) __weak weakSelf = self; The question is, when you call weakSelf.prop = @"string" in block, the setter setProp: will never be called. But if you define like…
nickcheng
  • 516
  • 4
  • 22
1
vote
1 answer

Why is ObjC block released when it captures a variable and not when it does not?

Example: extern void _objc_autoreleasePoolPrint(); int main(int argc, char *argv[]) { @autoreleasepool { id __weak blk; { int a = 10; blk = ^(NSString *msg){ NSLog(@"msg: %@", msg); …
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
1
vote
1 answer

Lazy Loading User Image UITableView - Updating the Image in Request Block not Working

So I am trying to lazyload a user pictures for a custom UITableView (BubbleTableView) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; AFImageRequestOperation…
Alan
  • 9,331
  • 14
  • 52
  • 97
1 2 3
99
100