iOS Strong and copy

Source: Internet
Author: User

Deep copy and shallow copy

Deep copy and shallow copy are mainly for class type, shallow copy is pointer copy, deep copy is Object copy.

Strong and copy of property

In contact with the iOS program often see the program some class type properties are strong decorated, some are copied modified, just start contact with some doubts, and later did not know how to form such a concept, for mutable type, with copy modification, for immutable type, With strong modification, it seems that this big Earth is wrong now.

Creating attributes is a mechanized work: For general properties, you declare them as nonatomic. By default, the object property is strong, and the scalar property is assign. One exception, however, is that for properties with a mutable copy, we tend to declare it as copy. For example, the type of the name attribute is NSString, and it is possible that someone created a person object and assigned a nsmutablestring name value to the property. Then, after a while, the mutable string was changed. If our property is not copy but strong, our person object will change as the variable string changes, which is not what we want to happen. This is the case for container classes like arrays or dictionaries.

Simply put, for the strong decorated property, when assigning to it is not a real copy, but just the right value of the retain Count plus 1, the left-hand pointer to the right value (also a pointer) point to the address, visually, that is, the value of the left and right value of the address value is the same , that is, a shallow copy;
However, for the property that is modified by copy and it follows the Nscopying protocol, when assigning a value to the property, the assignment statement does not retain the new value (the variable will not change with the variable string), and visually, the value of the left and right values may be different after the assignment. That is, the "assignment statement may be a deep copy, or it may be a shallow copy";
P.S: Why do you say " maybe " here? This issue is described below.
P.S: For a property assignment, it is wrong to simply understand that a shallow copy or deep copy operation is not correct, because the value of retain count is also changed (for shallow copies).

Give me a chestnut:

Two attributes, the former with copy decoration, the latter with strong @property (nonatomic, copy) NSString *firstname; @property (nonatomic, strong) NSString * LastName; nsmutablestring *str1 = [nsmutablestring stringwithstring:@ "Not bad"];self.firstname = str1;//Assignment//print left and right value of address value NSLog (@ "str1 Addr:0x%lx ", str1); NSLog (@ "Self.first addr:0x%lx", self.firstname); NSLog (@ ""); nsmutablestring *STR2 = [nsmutablestring stringwithstring:@ "Zhang"];self.lastname = str2;//Assignment//print left and right value of address value NSLog (@ "str2 Addr:0x%lx ", str2); NSLog (@ "LastName addr:0x%lx", self.lastname);

The printing results are:

str1 Addr:0x7967f580self.first addr:0x7967f1d0str2 addr:0x7967f460lastname addr:0x7967f460

Obviously, for a copy-modified FirstName, its assignment is a deep copy, and for LastName decorated with strong, the assignment is a shallow copy.

Back to explain the " possible " above.

In fact, for an assignment statement self.firstName = str1; (FirstName is modified by copy), if the Rvalue str1 is mutable, the statement is handled in a deep copy. Because if STR1 is mutable and performs a shallow copy, it means that if str1 changes, Then Self.firstname will also change, which we do not want, so it is reasonable to perform a deep copy, if the Rvalue str1 is immutable, then the assignment statement is still a shallow copy, because STR1 will not change again, the Self.firstname value will not be unexpected The deep copy is a waste of time.

P.s:objective-c's design concept is a bit verbose, and the smarter way to deal with it is when the str1 changes, then deep copy processing, many modern languages such as Python.

Summary: The property's keyword copy is intended to protect the "immutable property from being affected by a change that may occur after the Mutable Rvalue object has been assigned a value," and for the mutable attribute, there is no such problem.

iOS Strong and copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.