It’s not the right time to talk about this at 9102 years. But recently in some technical groups, but because of this problem burst into flames, both sides of the argument, no one can convince who. So, should I declare NSString attributes strong or copy? Take a look at this story:

Xiao Zhang wrote a library containing this code:

One day, Xiao Wang used the library.

Xiao Zhang, there is something wrong with your library.

Zhang: What’s the problem?

Xiao Wang: You declared the name attribute as strong, should use copy! [laughing]

Zhang: Why should you use copy?

Xiao Wang: Look at this code. I have changed the value of mStr, but the value of Tom. name has also changed!

Xiao Zhang: Yes, it has. What’s wrong with that?

Xiao Wang: Don’t you see the problem? I didn’t want tom.name to change, but now it has!

Zhang: You did it yourself. If you don’t want its value to change, why change it?

Xiao Wang: If you declare the name attribute as copy, it won’t change!

Zhang: But how do I know if you want to change? It’s up to you to change this value. Inside the library, I just read, not modify.

Xiao Wang: Anyway, now there’s a problem, that’s your problem!

Zhang: you created the mutable object yourself, and then you changed its value yourself, why is it my problem?

Xiao Wang: You mean I should shake the pot?

A big battle……

At this time, Xiao Li came over to understand the situation

Xiao Li: Xiao Wang, why did you write your code like this?

Xiao Wang: Why not?

Xiao Li: As programmers, we must know what happens to our code! Your code, logically speaking, looks something like this

Xiao Wang: How is this the same? One reassigned and one didn’t!

Li: Reassignment is to point to the new memory space, while you modify the contents of the mutable object mStr is to directly change the contents of the original memory space, both are modified content, but in a different way. The most important thing is that you (the caller) apply for this piece of memory, and in accordance with the principle of “he who requests, he who manages”, it is up to you (the caller) to manage whether the contents of this piece of memory are modified.

Sigh with emotion: it is oneself think, oppose dogmatism, book doctrine.