Strong and copy of the difference problem description
When defining the property of a class, it is important to select strong or copy for the property, and it is well understood that the copy attribute is best used to decorate if it is nsstring or Nsarray and its subclasses. Why is it? This is to prevent variable data from being assigned to it, and the property will change if the variable data changes.
code example
Or a combination of code to illustrate the situation
@interfacePerson:NSObject@property (Strong,nonatomic)Nsarray *bookarray1;@property (Copynonatomic)nsarray *bookarray2; @end @implementation person//omit setter method @end //person call Main () {nsmutablearray *books = [@[@" Book1 "] mutablecopy]; Person *person = [[Person alloc] init]; Person.bookarray1 = books; Person.bookarray2 = books; [Books Addobject:@ "Book2"]; nslog (@ "bookarray1:%@", person .bookarray1); nslog (@ "bookarray2:%@", person .bookarray2),
We see that the person.bookarray1 output using the strong adornment is [BOOK1,BOOK2], while the person.bookarray2 output using the copy adornment is [Book1]. You can see the difference here.
Remark: Using Strong, Person.bookarray1 and variable array books point to the same piece of memory area, books content changes, resulting in person.bookarray1 content changes, because the two are the same thing, and using copy,person.bookarray2 before assignment, will books Content replication, creating a new area of memory, so the two are not the same, books changes will not lead to person.bookarray2 changes.
In the final analysis, it is actually different modifiers, corresponding to different setter methods,
1. Strong corresponding setter method is to _property first release (_property release), then the parameters retain (property retain), and finally _property = property.
2. Copy corresponding setter method, is to _property first release (_property release), and then copy the parameter content (property copy), create a new memory address, and finally _property = property.
The difference between strong and copy