objective-c-blocks

Completion Handlers for Objective-C to Swift

 ̄綄美尐妖づ 提交于 2019-12-19 04:51:34
问题 I am currently rewriting a project from the Objective-C to Swift. Most of the project is done, but I am having problems translating a method that has a completion handler. I have reviewed the documentation, but I am still having problems. The method is: - (void)start:(void ( ^ ) ( WTStartupConfiguration *configuration ))startupHandler completion:(void ( ^ ) ( BOOL isRunning , NSError *error ))completionHandler In objective-C, I would simply write is as: [self.architectView start:^

Why is a __block variable is moved to the heap BEFORE the block is copied? [closed]

烈酒焚心 提交于 2019-12-19 01:18:07
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I know that a __block variable will be moved to the heap from the stack if a Block accessing it was copied. But the following test code show me that the

Asynchronously dispatched recursive blocks

不打扰是莪最后的温柔 提交于 2019-12-18 20:06:09
问题 Suppose I run this code: __block int step = 0; __block dispatch_block_t myBlock; myBlock = ^{ if(step == STEPS_COUNT) { return; } step++; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); }; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); The block is invoked once from outside. When the inner invocation is reached,

Asynchronously dispatched recursive blocks

大城市里の小女人 提交于 2019-12-18 20:06:03
问题 Suppose I run this code: __block int step = 0; __block dispatch_block_t myBlock; myBlock = ^{ if(step == STEPS_COUNT) { return; } step++; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); }; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); The block is invoked once from outside. When the inner invocation is reached,

Understand one edge case of block memory management in objc

六眼飞鱼酱① 提交于 2019-12-18 17:33:45
问题 the code below will crash because of EXC_BAD_ACCESS typedef void(^myBlock)(void); - (void)viewDidLoad { [super viewDidLoad]; NSArray *tmp = [self getBlockArray]; myBlock block = tmp[0]; block(); } - (id)getBlockArray { int val = 10; //crash version return [[NSArray alloc] initWithObjects: ^{NSLog(@"blk0:%d", val);}, ^{NSLog(@"blk1:%d", val);}, nil]; //won't crash version // return @[^{NSLog(@"block0: %d", val);}, ^{NSLog(@"block1: %d", val);}]; } the code runs in iOS 9 with ARC enabled. And I

Understand one edge case of block memory management in objc

好久不见. 提交于 2019-12-18 17:33:06
问题 the code below will crash because of EXC_BAD_ACCESS typedef void(^myBlock)(void); - (void)viewDidLoad { [super viewDidLoad]; NSArray *tmp = [self getBlockArray]; myBlock block = tmp[0]; block(); } - (id)getBlockArray { int val = 10; //crash version return [[NSArray alloc] initWithObjects: ^{NSLog(@"blk0:%d", val);}, ^{NSLog(@"blk1:%d", val);}, nil]; //won't crash version // return @[^{NSLog(@"block0: %d", val);}, ^{NSLog(@"block1: %d", val);}]; } the code runs in iOS 9 with ARC enabled. And I

Is this a sane Objective-C Block Implementation?

青春壹個敷衍的年華 提交于 2019-12-18 17:07:15
问题 I wanted a variation of NSRegularExpression's – stringByReplacingMatchesInString:options:range:withTemplate: method that takes a block instead of a template. The return value of the block would be used as the replacement value. This is more flexible than a template, as you can imagine. Sort of like using the /e modifier in Perl regular expressions. So I wrote a category to add the method. This is what I came up with: @implementation NSRegularExpression (Block) - (NSString *

Is this a sane Objective-C Block Implementation?

时间秒杀一切 提交于 2019-12-18 17:06:08
问题 I wanted a variation of NSRegularExpression's – stringByReplacingMatchesInString:options:range:withTemplate: method that takes a block instead of a template. The return value of the block would be used as the replacement value. This is more flexible than a template, as you can imagine. Sort of like using the /e modifier in Perl regular expressions. So I wrote a category to add the method. This is what I came up with: @implementation NSRegularExpression (Block) - (NSString *

Use of Blocks crashes app in iPhone Simulator 4.3/XCode 4.2 and 4.0.2

有些话、适合烂在心里 提交于 2019-12-18 16:48:16
问题 Anybody else having trouble with the 4.3 iPhone Simulator in XCode 4.2(lion) or 4.0.2? I have code that has long been working, tested, and in production that uses blocks to specify completion actions. For example, I use UIView animate to fade out some text on top of the label as follows: [UIView animateWithDuration: 0.0 delay: 0.0 options: (UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone) animations: ^{ videoTextLabel1.alpha = 0.0; videoTextLabel2.alpha = 0.0;

Passing block parameter that doesn't match signature

你离开我真会死。 提交于 2019-12-18 13:33:21
问题 I'm working with a block-based API and stumbled across a scenario where I was passing in a block parameter that had a signature that didn't match the typedef'd parameter the method was expecting. To my surprise, the compiler didn't seem to care about this, and the app didn't crash. Is this expected behavior? Example: typedef void(^MyBlock)(); typedef void(^MyBlockWithParam)(id param); - (void)doWork { MyBlockWithParam block1 = ^(id param) { NSLog(@"block1: %@", param); }; MyBlock block2 = ^{