objective-c-blocks

iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle”

半腔热情 提交于 2019-12-04 18:26:50
问题 I am very new to programming and Objective-C and I am trying to work out what is wrong with my code. I have read a bit about blocks but I don't know how any of what I have read so far is relevant to my code. My code is using the iOS 5 Twitter Framework. I use most of the sample code that Apple provides so I actually had no clue at first that I was using a block for the completion handler. Now I get those two messages from Xcode 4 saying " 1. Block will be retained by an object strongly

Understanding complex block syntax

冷暖自知 提交于 2019-12-04 17:48:23
问题 I'm a beginner to Objective C and iOS development, but a 13-year .NET veteran. I'm having a hard time mentally diagramming the following declaration, which came from the Programming with Objective C guide: void (^(^a)(void (^) (void))) (void) = ... It's used as an example of why one should use typedef to define blocks, but I want to understand what I'm looking at to better get a feel for block definition syntax in the first place. Here's how I've diagrammed it so far: Where I'm running into

Using blocks causes EXC_BAD_ACCESS

给你一囗甜甜゛ 提交于 2019-12-04 17:10:46
I'd like to use blocks, but it causes me a EXC_BAD_ACCESS after a few calls. My code: - (void) sendBasket { if (currentSendToBasketBlock != nil) { // there's already a webservice going... set the new one as waiting waitingSendToBasketBlock = ^ { WebServicesModel *webServicesModel = [[[WebServicesModel alloc] init] autorelease]; webServicesModel.delegate = self; [webServicesModel sendBasketToServer:currentBasket]; [self showBasketBackground]; }; [waitingSendToBasketBlock copy]; } else { currentSendToBasketBlock = ^ { WebServicesModel *webServicesModel = [[[WebServicesModel alloc] init]

Block recursion and breaking retain cycle

别说谁变了你拦得住时间么 提交于 2019-12-04 16:15:42
问题 To better illustrate the question, consider the following simplified form of block recursion: __block void (^next)(int) = ^(int index) { if (index == 3) { return; } int i = index; next(++i); }; next(0); XCode (ARC-enabled) warns that " Capturing 'next' strongly in this block is likely to lead to a retain cycle ". Agreed. Question 1 : Would the retain cycle be successfully broken by setting the block itself to nil , in this fashion: __block void (^next)(int) = ^(int index) { if (index == 3) {

Reference Counting of self in Blocks

∥☆過路亽.° 提交于 2019-12-04 13:03:11
I'm trying to get my head around how object lifetime and reference counting interact with code blocks. In the following code I'm just doing a simple animation that flashes as the top view on a UINavigationController's stack is swapped. The tricky part is that the popped view controller is the one where this code is defined . [UIView animateWithDuration:0.2 animations:^{self.navigationController.view.alpha = 0.0;} completion:^(BOOL finished){ UINavigationController *navController = self.navigationController; [self.navigationController popViewControllerAnimated:NO]; [navController

How to update UI in a task completion block?

五迷三道 提交于 2019-12-04 12:39:55
In my application, I let a progress indicator starts animation before I send a HTTP request. The completion handler is defined in a block. After I get the response data, I hide the progress indicator from inside the block. My question is, as I know, UI updates must be performed in the main thread. How can I make sure it? If I define a method in the window controller which updates UI, and let the block calls the method instead of updating UI directly, is it a solution? Also, if your app targets iOS >= 4 you can use Grand Central Dispatch: dispatch_async(dispatch_get_main_queue(), ^{ // This

Returning block that lives on the local stack

扶醉桌前 提交于 2019-12-04 12:36:01
The clang analyzer can check for returning stack-based memory. dispatch_block_t getPrintBlock (const char *msg) { return ^{ printf("%s", msg); }; } raises this error: returning block that lives on the local stack What does this error mean? You need to make a copy of the block to move it to the heap. i.e. something like: dispatch_block_t createPrintBlock (const char *msg) { return Block_copy(^{ printf("%s", msg); }) ; } CRD The error means you are returning a value which will be invalid after the method returns. This is not just an issue with blocks, consider: - (int *) badMethod { int

Is self retained within this Objective-C block?

送分小仙女□ 提交于 2019-12-04 12:35:30
问题 When I have a block in Objective-C that looks like this: self.request = [[ASIHTTPRequest requestWithURL:... [self.longPollRequest setCompletionBlock:^{ NSLog(@"%@", self.request.responseString); }]; will it retain self or explicitly retain self.request ? 回答1: As the Block Programming Topics says: In a reference-counted environment, by default when you reference an Objective-C object within a block, it is retained. This is true even if you simply reference an instance variable of the object.

NSComparisonResult and NSComparator - what are they?

岁酱吖の 提交于 2019-12-04 11:45:03
问题 What is NSComparisonResult and NSComparator ? I've seen one of the type definitions, something like that: typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); Is it any different from a function pointer? Also, I can't even guess what the ^ symbol means. 回答1: ^ signifies a block type , similar in concept to a function pointer. typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); // ^ ^ ^ // return type of block type name arguments This means that the type NSComparator is a

Performing block operations on objects in array and completing when all complete

北战南征 提交于 2019-12-04 11:05:36
I have an array of objects on which I would like to perform block operations. I am not sure the best way to do this. I am doing something like in the code below but I don't think this is the best practice. What is the best way to do such an operation? - (void)performBlockOnAllObjects:(NSArray*)objects completion:(void(^)(BOOL success))completionHandler { NSInteger counter = objects.count; for (MyObject *obj in objects) { [obj performTaskWithCompletion:^(NSError *error) { counter--; if (counter == 0) { completionHandler(YES); } }]; } } Typically you'd use dispatch groups for this. You "enter"