objective-c-blocks

EXC_BAD_ACCESS invoking a block

北慕城南 提交于 2019-12-01 06:14:54
问题 UPDATE | I've uploaded a sample project using the panel and crashing here: http://w3style.co.uk/~d11wtq/BlocksCrash.tar.gz (I know the "Choose..." button does nothing, I've not implemented it yet). UPDATE 2 | Just discovered I don't even have to invoke anything on newFilePanel in order to cause a crash, I merely need to use it in a statement. This also causes a crash: [newFilePanel beginSheetModalForWindow:[windowController window] completionHandler:^(NSInteger result) { newFilePanel; // Do

Is there an advantage to using blocks over functions in Objective-C?

雨燕双飞 提交于 2019-12-01 06:03:08
I know that a block is a reusable chunk of executable code in Objective-C. Is there a reason I shouldn't put that same chunk of code in a function and just called the function when I need that code to run? It depends on what you're trying to accomplish. One of the cool things about blocks is that they capture local scope. You can achieve the same end result with a function, but you end up having to do something like pass around a context object full of relevant values. With a block, you can do this: int num1 = 42; void (^myBlock)(void) = ^{ NSLog(@"num1 is %d", num1); }; num1 = 0; // Changed

block is likely to lead a retain cycle [duplicate]

a 夏天 提交于 2019-12-01 05:54:53
问题 This question already has an answer here : Blocks retain cycle from naming convention? (1 answer) Closed 5 years ago . I've written the following category for NSOperationBlock @implementation NSOperationQueue (Extensions) -(void)addAsynchronousOperationWithBlock:(void (^)(block))operationBlock { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); block signal = ^ { dispatch_semaphore_signal(semaphore); }; [self addOperationWithBlock:^{ operationBlock(signal); dispatch_semaphore

Using blocks to pass data back to view controller

冷暖自知 提交于 2019-12-01 05:12:05
问题 I was looking at this question. One of the answers shows how to use blocks to pass data backwards view the prepareForSegue method. My understanding is this method should really be used to pass data forward, not backwards. I'm wanting to try blocks out for this purpose - passing data back to another viewController. My question is: How do I do this, without using prepareForSegue method? I could use for instance in a UITableView - didselectRowAtIndexPath and dismiss the view - but then how does

Crash when using (animation) blocks in switch statements

不打扰是莪最后的温柔 提交于 2019-12-01 05:01:19
问题 This code works (if-statement with animations): // works if (_camOrientation == UIDeviceOrientationPortrait) { [UIView animateWithDuration:0.5f animations:^(void){ [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(0.0))]; }]; } else if (_camOrientation == UIDeviceOrientationLandscapeLeft) { [UIView animateWithDuration:0.5f animations:^(void){ [_distanceView setTransform:CGAffineTransformMakeRotation(degreesToRadian(90.0))]; }]; } This also works (switch-statement

objective-c block vs selector. which one is better?

无人久伴 提交于 2019-12-01 04:46:53
问题 In objective-c when you are implementing a method that is going to perform a repetitive operations, for example, you need to choice in between the several options that the language brings you: @interface FancyMutableCollection : NSObject { } -(void)sortUsingSelector:(SEL)comparator; // or ... -(void)sortUsingComparator:(NSComparator)cmptr; @end I was wondering which one is better ? Objective-c provides many options: selectors, blocks, pointers to functions, instances of a class that conforms

How do Clang 'blocks' work?

Deadly 提交于 2019-12-01 04:26:13
http://clang.llvm.org/docs/BlockLanguageSpec.txt Looks really cool. However, I don't understand it. I don't see examples it. I don't see examples of ideas hard to express in C++ as is, but trivial to express in blocks. Can anyone enlighten me on this? Blocks are, essentially, a way to pass code and scope around as data. They're known in some other languages as closures and anonymous functions. Here's an article with more details and code examples. NanoTech already linked to an explanation of blocks. As for how this relates to C++ let me state my personal opinion: This extension is not useful

Is there an advantage to using blocks over functions in Objective-C?

我怕爱的太早我们不能终老 提交于 2019-12-01 03:56:28
问题 I know that a block is a reusable chunk of executable code in Objective-C. Is there a reason I shouldn't put that same chunk of code in a function and just called the function when I need that code to run? 回答1: It depends on what you're trying to accomplish. One of the cool things about blocks is that they capture local scope. You can achieve the same end result with a function, but you end up having to do something like pass around a context object full of relevant values. With a block, you

Why is Clang confused by @try{} in a block with no return statement?

允我心安 提交于 2019-12-01 03:51:26
Under normal conditions, when a block is declared to return a value, but no return statement actually appears in the block, Clang fails to compile it with an error (of a missing return value). However, this breaks when that block contains @try{} @catch(...){} or @try{} @finally{} . Does anyone know why? The way I found this was when using @weakify() and @strongify() in RACExtScope in ReactiveCocoa, in one block I forgot to return a signal. But the compiler didn't warn me and crashed on runtime, which lead me to dig into it, preprocess the code and find that this causes it. Any explanation

How do Clang 'blocks' work?

人走茶凉 提交于 2019-12-01 02:00:37
问题 http://clang.llvm.org/docs/BlockLanguageSpec.txt Looks really cool. However, I don't understand it. I don't see examples it. I don't see examples of ideas hard to express in C++ as is, but trivial to express in blocks. Can anyone enlighten me on this? 回答1: Blocks are, essentially, a way to pass code and scope around as data. They're known in some other languages as closures and anonymous functions. Here's an article with more details and code examples. 回答2: NanoTech already linked to an