Difference between save and copy?

What is the difference between saving and copying? What is its significance in link counting?

I know that when an object is distributed using alloc / keep, the reference count is incremented by one. so what about using copy?

Another question related to this is the difference between using @property (nonatomic, keep) and @property (non-atomic, copy)?

+4
source share
3 answers

Save - runs on the created object, it simply increases the reference counter.

copy - create a new object

+10
source

Answering your question as far as I know. First, what is the difference between saving and copying? What is its significance in link counting?

save - "Indicates that the save should be called on the object when it is assigned .... The previous value is sent a release message." So you can imagine the purpose of an NSString instance (which is an object and which you probably want to keep). Thus, the retention rate is increased by 1.

copy - "Indicates that a copy of the object should be used for the assignment ... The previous value is sent by the release message." Basically the same as saving, but sending is copy, not service. if I remember correctly, the count will also increase by 1.

ok, now in more detail.

Property attributes are special keywords to tell the compiler how to create getters and setters. Here you specify two property attributes: nonatomic, which tells the compiler not to worry about multithreading and save, which tells the compiler to save the passed variable before setting the instance variable.

In other situations, you can use the “assign” property attribute instead of the save, which tells the NOT! Compiler to save the passed variable. Or perhaps the copy property attribute, which before copying, converts a copy of the passed variable.

Hope this helps. I found another entry here that could also help you.

Goal C - Assign, Copy, Save

Hooray! Jose

+3
source

Generally speaking, copy creates a new object that has the same value as the original object, and sets the reference count of the newly created object to 1 (by the way, the reference counter does not affect the original object).

However, copy equivalent to retain for an immutable object, which JUST increments the reference count of the original object by 1.

+2
source

Source: https://habr.com/ru/post/1337452/


All Articles