What is the role of β€œcopy” in ARC

What is the role of the copy keyword in ARC

I find that copy (keyword) can be used in ARC, and retain and release cannot be used. Moreover, is the copy role in ARC the same as the copy role in MRC? If yes, does copy retainCount +1 in ARC?

And I saw mike ash's blog about ARChe:

you need to explicitly copy the blocks you pass as id parameters:

  [myArray addObject: [^{ DoSomethingMagical(); } copy]]; 

but when I test such code (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 this mean that there is no need for a copy block when it is used as an id type?

+7
source share
3 answers

Yes, the copy role in ARC is the same as the copy role in MRR

copy will call the copyWithZone: method. when it sends a mutable object, it returns an immutable object clone, which saves Count equal to 1. When it sends an immutable object, it will not copy, it will return the object itself, but saveCount +1.

So, when you use a copy in ARC, you can use it like this: object1 = [object2 copy]; ARC will manage keepCount object1 , and then object1 will be released ARC, object2 preserveCount will be the corresponding change. Therefore you are not worried about memory.

The example block should be copy when passing as id parameters.

apple document said:

As a rule, you do not need to copy (or save) a block. Only you need to make a copy when you expect the block to be used after destroying the sphere in which it was declared. Copying moves block to heap

and ughoavgfhw said :

Blocks are similar to other objects for memory management, but not the same. When a block is created that accesses local variables, it is created on the stack. This means that it is valid only as long as its scope is. To save this block later, you need to copy it, copy it to the heap

+5
source

A copy does what the name implies. It returns a copy of the object. Say you have a mutable object, and you have a pointer to it. Then you reference it somewhere else in the code, and ARC will assume that you want to point to the same object and save it for you. In this case, if you change either of them, both will reflect the changes. If you point to it using copy , you will get a new object, and changing it will not affect the other:

 NSMutableArray* objA = [NSMutableArray new]; NSMutableArray* objB = objA; /* adding an object to either will be in both since they point to the same object*/ objB = [objA mutableCopy]; /*adding an object to one WILL NOT affect the other, they are different, distinct objects*/ 

In the docs here:

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW1

It mentions that when copying blocks, all of their Objective-C objects get a strong reference so that they do not go beyond before the block was executed.

+4
source

When you use copy in ARC, it does the same as without ARC, i.e. creates a copy of the object that receives the copy (it creates an instance of a new object in memory), and creates a strong relation to the object that executes the code.
This means that the copy of the object now has a save value of 1, but this is not relevant to you in ARC, since it is processed automatically.

+1
source

All Articles