objective-c-blocks

“Missing __block type specifier” compilation error after refactoring code but ok before refactor

只愿长相守 提交于 2019-12-04 07:49:41
I have some code that downloads an image and assigns it within a block. The code currently works however I want to refactor it into a separate method, however after the refactoring I get a compilation error. This is the original code which compiles and runs with the downloaded image being assigned successfully: - (void) someMethod { … MyObject* object = [[MyObject alloc] init]; [self.objects addObject: object]; NSString* graphRequest = [NSString stringWithFormat:@"%@%@%@", @"https://graph.facebook.com/", fbid, @"/picture?type=square"]; FBRequest *fbRequest = [FBRequest requestForGraphPath:

beginSheet: block alternative with ARC?

独自空忆成欢 提交于 2019-12-04 07:27:18
Mike Ash created an example of using blocks to handle callbacks from sheets, which seems very nice. This was in turn updated to work with garbage collection by user Enchilada in another SO question at beginSheet: block alternative? , see below. @implementation NSApplication (SheetAdditions) - (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block { [self beginSheet:sheet modalForWindow:docWindow modalDelegate:self didEndSelector:@selector(my_blockSheetDidEnd:returnCode:contextInfo:) contextInfo:Block_copy(block)]; } - (void)my

iOS 4 blocks and retain counts

房东的猫 提交于 2019-12-04 07:25:58
问题 I'm just getting started with blocks and Grand Central Dispatch. I've been told (and read in the Apple Documentation) that any object referenced from within a block gets retained. For instance: ^{ self.layer.transform = CATransform3DScale(CATransform3DMakeTranslation(0, 0, 0), 1, 1, 1); self.layer.opacity = 1; } "self" gets retained so it leaks. To avoid that, I need to assign self to: __block Object *blockSelf = self; and then use blockSelf instead of self inside my block. My question is:

static variables inside Objective-C blocks?

℡╲_俬逩灬. 提交于 2019-12-04 07:13:23
I'm working on some CoreAnimation stuff. A navigation controller with a few view controllers. And the view controllers have UISCrollViews for different "pages" of a "brochure." On each page, there might be some animation that gets triggered when the user flips to that page. I was trying something like this (for one-shot animations). void (^animationBlock)() = ^{ static bool alreadyTriggered = NO; if(alreadyTriggered) return; [CATransaction begin]; [CATransaction setCompletionBlock: ^{ alreadyTriggered = YES; }]; // Do me some animations... [CATransaction commit]; }; NSMutableDictionary*

__block variable returning nil after block runs [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-04 06:56:30
问题 This question already has answers here : Method returning value from asynchronous block with FacebookSDK (3 answers) Return value for function inside a block (3 answers) Closed 2 years ago . I have a created a string that I have prefixed with the __block identifier so that I am able to access that variable outside of my block. However once I attempt to run the application the variable returns 'nil': -(void)downloadParcelData { __block NSString *test; MKMapRect mRect = self.mapView

How to add activity indicator to the image in UITableView?

一个人想着一个人 提交于 2019-12-04 06:25:41
问题 I have created the Custom tableView and using custom tableView class in Other class to Show the tableView .. I am Loading the image from Server and displaying it in the tableViewRow .. each image is different in it.. On Screen only 7 out of 10 images will come, and when I scroll down the first 3 images are repeating for some time and when those images get load its showing proper images.. but initially till new images will come its showing old images, I want to put a activity indicator to show

Internal implementation of UIView's block-based animation methods

╄→尐↘猪︶ㄣ 提交于 2019-12-04 06:03:46
Ever since their introduction in iOS 4, I have been wondering about the internal implementation of the UIView 's block-based animation methods. In particular I would like to understand what mystical features of Objective C are used there to capture all the relevant layer state changes before and after execution of the animation block. Observing the black-box implementation, I gather that it needs to capture the before-state of all layer properties modified in the animation block, to create all the relevant CAAnimations . I guess it does not do a snapshot of whole view hierarchy, as that would

Is it possible to define a block as a member of a class?

可紊 提交于 2019-12-04 05:39:21
I'm trying to implement a very simple strategy class in Objective-C that allows for strategies to be defined inline instead of being defined through inheritance. Currently my code looks like this: @interface SSTaskStrategy : NSObject { (NSArray *)(^strategy)(void); } @end I thought this would work, but I'm getting the error Expected specifier-qualifier-list before '(' token Any ideas how to make this work? You should drop the parentheses around NSArray * in your ivar definition: @interface SSTaskStrategy : NSObject { NSArray * (^strategy)(void); } @end Also, I highly recommend that you use a

Assigning objects to variable outside a block

▼魔方 西西 提交于 2019-12-04 03:56:57
The following code crashes, since the contents of sentence go away when the final block exits. #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // simple block test - just iterate over some items and // add them to a string NSArray *items = [NSArray arrayWithObjects:@"why ", @"must ", @"this ",nil]; __block NSString *sentence = @""; [items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { sentence = [sentence stringByAppendingFormat:@"%@",obj]; }]; // crash! NSLog(@"Sentence is %@"

In which thread are iOS completion handler blocks called?

Deadly 提交于 2019-12-04 03:27:48
For example, in GKScore 's reportScoreWithCompletionHandler ( documentation ), suppose you call [score reportScoreWithCompletionHandler:^(NSError *error) { // do some stuff that may be thread-unsafe }]; In which thread will the completion handler be called: the main thread, the same thread as reportScoreWithCompletionHandler was called, or a different thread (presumably the thread that the actual score reporting is done)? In other words, does the work done in the completion handler need to be thread-safe (as in, it doesn't matter what thread it's done in)? mlaster In practical terms it doesn't