Objective-C Blocks Quiz
2015-11-04 11:07
591 查看
http://blog.parse.com/learn/engineering/objective-c-blocks-quiz/
Do you really know how blocks work in Objective-C? Take this quiz to find out.
All of these examples have been verified with this version of LLVM:
This example
always works.only works with ARC.
only works without ARC.never works.
This example
always works.only works with ARC.
only works without ARC.never works.
This example
always works.only works with ARC.
only works without ARC.never works.
This example
always works.only works with ARC.
only works without ARC.never works.
This example
always works.only works with ARC.
only works without ARC.never works.
time a block outlives the stack frame where it is declared. That will force it to be copied to the heap as an
Haha! No, of course it’s not that simple. According to Apple:
Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use
other methods that do a retain.
But one of the LLVM maintainers later said:
We consider this to be a compiler bug, and it has been fixed for months in the open-source clang repository. What that means for any hypothetical future Xcode release, I cannot say.
So, hopefully Apple was describing a workaround for bugs that existed at the time their guide was written, and everything should work smoothly with ARC and LLVM in the future. But watch out.
Do you really know how blocks work in Objective-C? Take this quiz to find out.
All of these examples have been verified with this version of LLVM:
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.2 Thread model: posix
Example A
void exampleA() { char a = 'A'; ^{ printf("%cn", a); }(); }
This example
always works.only works with ARC.
only works without ARC.never works.
Example B
void exampleB_addBlockToArray(NSMutableArray *array) { char b = 'B'; [array addObject:^{ printf("%cn", b); }]; } void exampleB() { NSMutableArray *array = [NSMutableArray array]; exampleB_addBlockToArray(array); void (^block)() = [array objectAtIndex:0]; block(); }
This example
always works.only works with ARC.
only works without ARC.never works.
Example C
void exampleC_addBlockToArray(NSMutableArray *array) { [array addObject:^{ printf("Cn"); }]; } void exampleC() { NSMutableArray *array = [NSMutableArray array]; exampleC_addBlockToArray(array); void (^block)() = [array objectAtIndex:0]; block(); }
This example
always works.only works with ARC.
only works without ARC.never works.
Example D
typedef void (^dBlock)(); dBlock exampleD_getBlock() { char d = 'D'; return ^{ printf("%cn", d); }; } void exampleD() { exampleD_getBlock()(); }
This example
always works.only works with ARC.
only works without ARC.never works.
Example E
typedef void (^eBlock)(); eBlock exampleE_getBlock() { char e = 'E'; void (^block)() = ^{ printf("%cn", e); }; return block; } void exampleE() { eBlock block = exampleE_getBlock(); block(); }
This example
always works.only works with ARC.
only works without ARC.never works.
Conclusions
So, what’s the point of all this? The point is always use ARC. With ARC, blocks pretty much always work correctly. If you’re not using ARC, you better defensivelyblock = [[block copy] autorelease]any
time a block outlives the stack frame where it is declared. That will force it to be copied to the heap as an
NSMallocBlock.
Haha! No, of course it’s not that simple. According to Apple:
Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use
[^{} copy]when passing “down” the stack into
arrayWithObjects:and
other methods that do a retain.
But one of the LLVM maintainers later said:
We consider this to be a compiler bug, and it has been fixed for months in the open-source clang repository. What that means for any hypothetical future Xcode release, I cannot say.
So, hopefully Apple was describing a workaround for bugs that existed at the time their guide was written, and everything should work smoothly with ARC and LLVM in the future. But watch out.
相关文章推荐
- 如何使用swfobject(中文版)
- 黑马程序员-------Objective-C基础2
- Objective-C中变量采用@property的各个属性值的含义
- (原创)c#学习笔记09--定义类02--System.Object
- 全面解析Objective-C中的block代码块的使用
- Object C 第二讲
- iOS开发系列—Objective-C之Foundation框架
- Objective-C之Block
- Objective-C的Runtime
- Android学习记录之Volley框架JSONObjectRequest
- 关于特殊OU屏蔽查询
- Java记录 -57- Object的equals、hashcode和toString方法
- python运行不了 AttributeError: module object has no attribute getdefaultlocale
- object-c之NSdate浅析
- Objective-C中的属性指示符
- Hibernate, collections, and duplicate objects
- javaScriptObject转String
- Lesson 14 Usage as temporary objects
- Java API ——Object类
- iOS开发系列—Objective-C之内存管理