OC Details, details of Public gun battles

Source: Internet
Author: User

OC Details, details of Public gun battles

Overview

  • Copy: copy an object with the same content as the source object
  • To copy data, you must comply with the following two Protocols:

    • NSCopying
    • NSMutableCopying
  • Types of objects returned for copying

    • Variable, the object returned by the mutableCopy message
    • Immutable. The object returned by the copy message
  • Copy type

    • The shortest copy only copies a pointer to the source object. No object is created and no memory is allocated.
    • Deep copy: Copies the source object, creates a new object, and allocates memory.
  • Note:

    • The system processes the memory of the container class object and the non-container Class Object differently. That is, when an object not strongly referenced by other objects is removed from the container, the object will be destroyed.
Copy and Retain
  • Copy

    • Is to create a new object, copy the content
    • Copy indicates that the content of the two objects is the same, and the reference count of the new object is 1.
    • It is irrelevant to the reference count of the old object, so the object does not change.
    • Copy reduces the context
  • Retain

    • Creates a pointer and copies the pointer.
    • The object address is the same, and the content is the same.
    • Object reference count + 1
Copying behavior of different objects
  • Non-container object (For example, NSString))

    • For immutable objects

      • Rules

        • Copy, shortest copy (pointer copy)
        • MutableCopy: Deep copy (Object copy). The returned object is variable (new variable object is generated)
      • Example

        -(Void) imutableInstanceCopy {NSString * string = @ "Welcome to Xcode"; // copy NSString * stringCopy = [string copy]; // mutableCopy, returned object variable NSMutableString * stringMutableCopy = [string mutableCopy]; [stringMutableCopy appendString :@"! "]; // The memory address of string and stringCopy is the same as that of NSLog (@" string: % p ", string); NSLog (@" strongCopy: % p ", stringCopy ); // The memory address of string and stringMutableCopy is different. A new memory NSLog (@ "stringMCopy: % p", stringMutableCopy) is allocated );}
    • For mutable objects

      • Rules

        • Copy, deep copy (Object copy), the returned object is unchangeable
        • MutableCopy, deep copy (Object copy)
      • Example

        -(Void) mutableInstanceCopy {NSMutableString * mutableString = [NSMutableString stringWithString: @ "Welcome to Xcode"]; // The returned object is an immutable NSString * stringCopy = [mutableString copy]. NSMutableString * mutableStringCopy = [mutableString copy]; // This statement reports an error during runtime. The error message "Attempt to mutate immutable object with appendString:" [mutableStringCopy appendString :@"~~~ "]; // Deep copy, returns the object variable NSMutableString * stringMutableCopy = [mutableString mutableCopy]; [stringMutableCopy appendString :@"! "]; // The memory addresses of the three and mutableString are different NSLog (@" mutableString: % p ", mutableString); NSLog (@" string: % p ", stringCopy ); NSLog (@ "mutableStringCopy: % p", mutableStringCopy); NSLog (@ "stringMutbleCopy: % p", stringMutableCopy );}
  • Container object (NSArray)

    • Follow the non-container object copy Principle
    • Note:

      • The elements in the container are pointer assignments (Shortest copy)
      • Example

        -(Void) containerInstanceShallowCopy {NSArray * array = [NSArray arrayWithObjects: [NSMutableString stringWithString: @ "Welcome"], @ "to", @ "Xcode", nil]; // NSArray * arrayCopy = [array copy]; // NSMutableArray * arrayMutableCopy = [array mutableCopy]; NSLog (@ "array: % p", array ); NSLog (@ "arrayCopy: % p", arrayCopy); NSLog (@ "arrayMutableCopy: % p", arrayMutableCopy); // objects in the container are shortest copies, that is, they only have one NSMutableString * testString = [array objectAtIndex: 0] in the memory; [testString appendString: @ "you"]; // NSLog (@ "array [0]: % @", array [0]); NSLog (@ "arrayCopy [0]: % @ ", arrayCopy [0]); NSLog (@" arrayMutableCopy [0]: % @ ", arrayMutableCopy [0]);}
    • Implement deep replication in the true sense

      -(Void) containerInstanceDeepCopy {NSArray * array = [NSArray arrayWithObjects: [NSMutableString stringWithString: @ "Welcome"], @ "to", @ "Xcode", nil]; // The object in the array is a pointer copy NSArray * deepCopyArray = [[NSArray alloc] initWithArray: array]; // The above real deep copy, the objects in the array are NSArray * trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData: [NSKeyedArchiver attributes: array]; NSLog (@ "array: % p", array ); NSLog (@ "deepCopyArray: % p", deepCopyArray); NSLog (@ "trueDeepCopyArray: % p", trueDeepCopyArray); // change the first element of array [[array objectAtIndex: 0] appendString: @ "you"]; // only affects the NSLog (@ "array [0]: % @", array [0]), the first element of the deepCopyArray. NSLog (@ "arrayCopy [0]: % @", deepCopyArray [0]); // the first element of the trueDeepCopyArray array is not affected, is a real deep copy NSLog (@ "arrayMutableCopy [0]: % @", trueDeepCopyArray [0]);}
  • Custom object

    • To copy an object, follow the NSCoping and NSMutableCoping protocols and implement the following methods:

      • -(Id) copyWithZone :( NSZone *) zone, variable copy
      • -(Id) mutableCopyWithZone :( NSZone *) zone, immutable copy
    • Example (copying a custom object Person)

      • Abide by the agreement and set Member attributes

        @ Interface Person: NSObject <NSCopying, NSMutableCopying>/** name */@ property (nonatomic, copy) NSMutableString * name;/** address */@ property (nonatomic, copy) NSString * address;/** age */@ property (nonatomic, assign) NSInteger age; @ end
      • Override Initialization Method

        -(Instancetype) init {if (self = [super init]) {self. name = [[NSMutableString alloc] initWithString: @ "XiaoYaowang"]; self. address = @ "Kong Shan new rain"; self. age = 3;} return self ;}
      • Implementation-(id) copyWithZone :( NSZone *) zone

        - (id)copyWithZone:(NSZone *)zone{    Person *p = [[[self class] allocWithZone:zone] init];    p.name = [self.name copy];    p.address = [self.address copy];    p.age =  self.age;    return p;}
      • Implementation-(id) mutableCopyWithZone :( NSZone *) zone

        -(Id) mutableCopyWithZone :( NSZone *) zone {Person * p = [[self class] allocWithZone: zone] init]; // note that the mutableCopy Method p is used here. name = [self. name mutableCopy]; p. address = [self. address copy]; p. age = self. age; return p ;}

--- Restore content end ---

Related Article

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.