iOS.Block.Best.Practices

1. 

http://blog.parse.com/2013/02/05/objective-c-blocks-quiz/

2. block的内部实现

http://www.idryman.org/blog/2012/09/29/c-objc-block-byref-internals/

3. block是应该copy还是retain呢?  (已读)

Copy vs retain for Objective-C blocks

http://blog.refractalize.org/post/10476042560/copy-vs-retain-for-objective-c-blocks 

4. Programming with C Blocks  (未读)

http://thirdcog.eu/pwcblocks/#objcblocks

5.

https://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html

http://blog.random-ideas.net/?p=160 (The correct way to avoid capturing self in blocks with arc) 

http://blog.mugunthkumar.com/articles/migrating-your-code-to-objective-c-arc/#Avoiding_early_releases_using___block

http://www.cocoanetics.com/2012/03/block-retain-loop/

http://zeroheroblog.com/tag/arc

http://patzearfoss.com/2012/05/11/a-quick-gotcha-about-blocks/

http://www.idryman.org/blog/2012/11/22/arc-best-practices-and-pitfalls/

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

 

https://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html

http://www.informit.com/blogs/blog.aspx?uk=Ask-Big-Nerd-Ranch-Blocks-in-Objective-C 

 

6. Blocks in objective C 

Todo: 该系列blog中的Reference还未读。

6.1 Block的语法

http://iosdevblog.com/2014/04/29/blocks-in-objective-c-part-1/

"Blocks: An Objective-C class defines an object that combines data with related behavior.

In other words it is a block of inline statements that can be passed around to other functions

as if it were an object."

int (^anotherBlock)(int, int);

anotherBlock = ^(int num1, int num2) {

        return num1+num2;

    };





int (^anotherBlock)(int, int) = ^(int num1, int num2) {

        return num1*num2;

    };



typedef int (^operationBlock)(int, int);

 

6.2 Block和变量的修改

http://iosdevblog.com/2014/05/06/blocks-in-objective-c-part-2/ 

"Don’t forget that blocks are also called closures in other languages." 

"Blocks have access to non-local variables but they cannot change them.

Blocks use them as read-only so for example:" 

1 int initialValue = 32;

2 int (^addToInitialValue)(int) = ^(int x) {

3     return initialValue + x;

4 };

5   

6 NSLog(@"%i", addToInitialValue(10)); // 42

7   

8 initialValue = 100;

9 NSLog(@"%i", addToInitialValue(10)); // Still 42. 

 

"So the block captures initialValue value at the time of its creation."

"If we want to modify a variable inside the block, we need to use __block modifier.

So using the same example:"

1 __block int initialValue = 32;

2 int (^addToInitialValue)(int) = ^(int x) {

3      return initialValue + x;

4 };

5      

6 NSLog(@"%i", addToInitialValue(10)); // 42

7      

8 initialValue = 100;

9 NSLog(@"%i", addToInitialValue(10)); // Now is 110.

Todo: 声明返回对象指针的block的例子。

 

6.3 Block作为方法的参数

http://iosdevblog.com/2014/05/07/blocks-in-objective-c-part-3/

"Block as parameters is more flexible than the standard data types. "

 1 //  Person.h

 2  

 3 #import <Foundation/Foundation.h>

 4  

 5 @interface Person : NSObject

 6  

 7 @property int age;

 8  

 9 - (void)celebrateBirthdayWithBlock:(void (^)(int))activity;

10  

11 @end

 

Block参数:"unlike block variables, this doesn’t require a name for the block,

just define the return type and the types of the parameters."

采用typedef:

 1 //  Person.h

 2  

 3 #import <Foundation/Foundation.h>

 4  

 5 typedef void(^Activity)(int);

 6  

 7 @interface Person : NSObject

 8  

 9 @property int age;

10  

11 - (void)celebrateBirthdayWithBlock:(Activity)activity;

12  

13 @end

 

 

7. Objective-C Succinctly: Blocks  [Todo]

http://code.tutsplus.com/tutorials/objective-c-succinctly-blocks--mobile-22030 

 

8. BlocksKit

https://zearfoss.wordpress.com/2012/03/02/do-just-about-anything-with-blocks/

 

Block引起的cycle retain

9. A Quick Gotcha About Blocks

https://zearfoss.wordpress.com/2012/05/11/a-quick-gotcha-about-blocks/

这个blog讲解了block引起的cycle retain问题。

Retain cycles occur when two objects in your application keep strong references to one another.

Blocks automatically scope capture and retain any object types you use in the block.

 

10. Programming with Objective-C / Avoid Strong Reference Cycles when Capturing self

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW16 

 

11. Block在ObjC中的类是什么呢?

NSBlock, 

A) How are NSBlock objects created?

B) http://www.cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html  (too old)

C) http://clang.llvm.org/docs/Block-ABI-Apple.html#blocks-as-objects 

"The compiler will treat Blocks as objects when synthesizing property setters and getters,

will characterize them as objects when generating garbage collection strong and weak layout

information in the same manner as objects, and will issue strong and weak write-barrier

assignments in the same manner as objects."

 

你可能感兴趣的:(block)