Defining Objective-C blocks as properties - best practice

前端 未结 3 843
面向向阳花
面向向阳花 2020-12-13 10:56

I\'ve recently come across an Apple document that shows the following property declaration for a block:

@interface XYZObject : NSObject
@property (copy) void         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 11:43

    By default blocks are created on the stack. Meaning they only exist in the scope they have been created in.

    In case you want to access them later they have to be copied to the heap by sending a copy message to the block object. ARC will do this for you as soon as it detects a block needs to be accessed outside the scope its created in. As a best practise you declare any block property as copy because that's the way it should be under automatic memory management.

    Read Stack and Heap Objects in Objective-C by Mike Ash for more info on stack vs. heap.

提交回复
热议问题