What is the role of the "copy" in the ARC
automatic-ref-counting, copy, ios, objective-c
Solution
Yes , the role of `copy` in ARC is the same as the role of `copy` in MRR
The `copy` will invoke the `copyWithZone:` method . when it send to the mutable object , it will return a clone immutable object , which retainCount is 1. When it send to the immutable object , it will not copy , it will return the object itself , but the retainCount +1.
So when you use copy in ARC , you can use like this : `object1 = [object2 copy];` ARC will manage the retainCount of the `object1` , when then `object1` is released by ARC , the `object2`'s retainCount will be a corresponding change. So you do not worry about the memory.
About the `block` need be `copy` when pass as `id` parameters.
the apple document said :
Typically, you shouldn’t need to copy (or retain) a block. You only need to make a copy when you expect the block to be used after destruction of the scope within which it was declared. Copying moves a block to the heap
and As ughoavgfhw said :
Blocks are similar to other objects for memory management, but not the same. When a block which accesses local variables is created, it is created on the stack. This means that it is only valid as long as its scope exists. To save this block for later, you must copy it, which copies it to the heap
Problem
What is the role of keyword `copy` in the ARC I find the `copy`(keyword) can be used in ARC while `retain` and `release` cannot be used. Moreover is the role of `copy` in ARC is the same as the role of `copy` in MRC? If yes , does `copy` make the `retainCount` +1 in ARC? And I have seen mike ash blog about ARChe said : you need to explicitly copy blocks that you pass as id parameters: ``` [myArray addObject: [^{ DoSomethingMagical(); } copy]]; ``` but when I test code like this (without using `copy`) it works well too. ``` NSArray *array = [[NSArray alloc] initWithObjects:^{NSLog(@"hahaha");}, nil]; [self test:[array objectAtIndex:0]]; - (void)test:(void (^)(void))completion { completion(); } ``` Does it mean that there is no need to `copy` block when it used as an `id` type?