iOS Learning (ii) deep copy & Shallow copy

Source: Internet
Author: User

  1, whether it is a deep copy or a shallow copy, the type of object being copied is constant. What functionality does this object type have, and does not change because the pointer type of the returned object is modified by itself.   For example: the value of STR and str1 here is exactly the same as the pointer address, because the immutable type uses copy and nothing is changed. Why can a pointer of type NSNumber point to an object of type nssring copy? Because the copy return type is the ID universal pointer type. If you return a instancetype, you will be warned. Remember: Using shallow copy copy for immutable temporary variables is useless, and the addresses and values are the same.  2, how to understand the last sentence? A temporary variable is a variable declared inside a method or function body, such as reading a plist file to a variable nsmutablearray type of Arraym, which is a variable temporary variable. When the data is organized, it is necessary to assign a value to the member variables of the class, such as an array of immutable nsarray types, which is an immutable member variable, which is more secure: Self.array = [Arraym copy];  in addition, If this member variable is nsstring, use the Set method when using the copy  above is the basic object type of OC deep copy and shallow copy. ==============================================, let's talk about the deep copy and shallow copy of the custom OC class object and its subclass objects such as a person class, its subclasses Student1, the parent class follows < Nscoping> Protocol 2, the parent class implements Copywithzone: Method 3, subclass student inherits the parent class person, automatically inherits the <NSCopying> protocol 4, the subclass student overrides the parent class Copywithzone: Method  ********************************************************************* Problem One: If the parent class's [self class] What is the problem with the parent class person itself type? It says that although the return value type of copy is of type ID, any type of pointer can point to the object in the copy heap, but there is no functional method that has no functional method at all. Here though the student type pointer S1 points to the person's copy object, the person's Copywithzone: method is personThe class itself is copied, so the S1 pointer is an object of type person, and the person class does not have a no member variable, so the method cannot be found. reported the following common error:  question two: If in Copywithzone: less write the assignment of one or some member variables, what will be the result? The result is that a member variable that is not assigned will maintain the default value for that member variable type. For example, a person has less than one age member variable assigned to it, and the student object gets a value of 0 instead of 1000 when the copy is accessed. As shown in debugging.  ////////////////////////////////////////////////////////////////////////: [Self class] in iOS is equivalent to T in C #

iOS Learning (ii) deep copy & Shallow 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.