Talking about the "depth copy" of ObjC and the "depth copy" of objc

Source: Internet
Author: User

Talking about the "depth copy" of ObjC and the "depth copy" of objc

The ObjC should have no deep copy concept, but I found this problem in actual use, that is, when object A and object B point to the same content in the memory at the same time, if object B changes the value, object A also changes the value accordingly. This leads to a problem, which is consistent with the idea of copying in depth in C ++. The general idea is this. Let's take a look at the code.

There are five files: Point. h Point. m Rectangle. h Rectangle. m and main. m.

Rectangle contains the Point object. Main is the main function.

// Point.h#import <foundation/Foundation.h>@interface XYPoint: NSObject@property int x, y;-(void) setX: (int) xVal andY: (int) yVal;@end

// Point.m#import "Point.h"@implementation XYPoint@synthesize x, y;-(void) setX: (int) xVal andY: (int) yVal{  x = xVal;  y = yVal;}@end


// Rectangle.h#import <Foundation/Foundation.h>@class XYPoint;@interface Rectangle: NSObject@property int width, height;-(XYPoint*) origin;-(void) setOrigin: (XYPoint*) pt;-(void) setWidth: (int) w andHeight: (int) h;-(void) setDeepOrigin: (XYPoint*) pt;@end

// Rectangle.m#import "Rectangle.h"#import "Point.h"@implementation Rectangle{  XYPoint* origin;}@synthesize width, height;-(void) setWidth: (int) w andHeight: (int) h{  width = w;  height = h;}-(void) setOrigin: (XYPoint*) pt{  origin = pt;}-(void) setDeepOrigin: (XYPoint*) pt{  if (!origin)    origin = [[XYPoint alloc] init];  [origin setX: [pt x] andY: [pt y]];}-(XYPoint*) origin{  return origin;}@end

// Main. m # import "Point. h "# import" Rectangle. h "int main (int argc, char * argv []) {@ autoreleasepool {Rectangle * rec = [[Rectangle alloc] init]; XYPoint * ptr = [[XYPoint alloc] init]; [ptr setX: 100 andY: 200]; [rec setWidth: 5 andHeight: 8]; [rec setDeepOrigin: ptr]; NSLog (@ "Rectangle w = % I, h = % I", [rec width], [rec height]); // rec. width is also OK. NSLog (@ "Origin at (% I, % I)", [[rec origin] x], [rec origin] y]); [ptr setX: 999 andY: 999]; NSLog (@ "Deep copy Origin at (% I, % I)", [[rec origin] x], [[rec origin] y]); [rec setOrigin: ptr]; [ptr setX: 999 andY: 999]; NSLog (@ "Shallow copy Origin at (% I, % I )", [[rec origin] x], [[rec origin] y]);} return 0 ;}


We can see the differences between the two copies.

Although ObjC does not have a clear concept of deep copy, the C ++ idea of deep copy can be transplanted to ObjC.

At the same time, it is worth noting that the concept of a C ++ combination (composition) is also used here, that is, Point is a member of Rectangle, in addition, after the @ class XYPoint is declared in the Rectangle interface, the header file of the Point does not need to be imported. By default, the system treats the Point as a class. However, the member functions and member variables of Point cannot be used, because the system only knows that it is a class, but does not know what its member functions and variables are, if you want to reference its member functions and variables, You need to import the header file of Point.

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.