IOS ObjectiveC中的赋值与对象拷贝

2020-01-21 01:21:54于海丽

二、对象拷贝

Foundation框架的NSObject类提供了两个方法,分别是copy和mutableCopy方法,用于对NSObject对象进行拷贝操作。copy方法会调用NSCopying协议的copyWithZone:方法,而mutableCopy会调用 NSMutableCopying协议的mutableCopyWithZone:方法。将上面的代码修改如下:


#import <Foundation/Foundation.h> 
 
@interface TestObject : NSObject 
{ 
  @public 
  int x; 
  int y; 
} 
@end 
 
@implementation TestObject 
- (NSString*)description 
{ 
  return [NSString stringWithFormat:@"%@: %p, x: %d, y: %d", [self class], self, x, y]; 
} 
@end 
 
typedef struct TestStruct 
{ 
  int x; 
  int y; 
} 
TestStruct; 
 
int main(int argc, const char * argv[]) 
{ 
  @autoreleasepool 
  {     
    TestObject* to1 = [[[TestObject alloc] init] autorelease]; 
    to1->x = 100; to1->y = 50; 
    TestObject* to2 = [[[TestObject alloc] init] autorelease]; 
    to2->x = 200; to2->y = 400; 
    TestObject* to3 = [[[TestObject alloc] init] autorelease]; 
    to3->x = 300; to3->y = 500; 
     
    //创建包含to1, to2, to3的数组array1 
    NSArray* array1 = [NSArray arrayWithObjects:to1, to2, to3, nil]; 
    NSLog(@"array1: %p, n%@", array1, array1); 
     
    //array2是array1调用copy的结果 
    NSArray* array2 = [array1 copy]; 
    NSLog(@"array2: %p, n%@", array2, array2); 
    [array2 release]; 
     
    //mutableArray2是array1调用mutableCopy的结果 
    NSMutableArray* mutableArray2 = [array1 mutableCopy]; 
    NSLog(@"mutableArray2: %@, %p, n%@", [mutableArray2 class], mutableArray2, mutableArray2); 
    [mutableArray2 removeLastObject]; 
     
    NSLog(@"After remove last object of mutableArray2"); 
     
    NSLog(@"array1: %p, n%@", array1, array1); 
    NSLog(@"array2: %p, n%@", array2, array2); 
    NSLog(@"mutableArray2: %p, n%@", mutableArray2, mutableArray2); 
     
    //mutableArray3是mutableArray2调用mutableCopy的结果 
    NSMutableArray* mutableArray3 = [mutableArray2 mutableCopy]; 
    NSLog(@"mutableArray3: %p, n%@", mutableArray3, mutableArray3); 
    [mutableArray2 release]; 
     
    //array4是mutableArray3调用copy的结果 
    NSArray* array4 = [mutableArray3 copy]; 
    NSLog(@"array4: %@, %p, n%@", [array4 class], array4, array4); 
    [mutableArray3 release]; 
    [array4 release]; 
  } 
  return 0; 
} 

程序的运行结果如下:


2012-03-22 19:20:49.548 ObjectCopy[18042:403] array1: 0x7f9071414820,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400", 
  "TestObject: 0x7f9071414230, x: 300, y: 500" 
) 
2012-03-22 19:20:49.550 ObjectCopy[18042:403] array2: 0x7f9071414820,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400", 
  "TestObject: 0x7f9071414230, x: 300, y: 500" 
) 
2012-03-22 19:20:49.551 ObjectCopy[18042:403] mutableArray2: __NSArrayM, 0x7f9072800000,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400", 
  "TestObject: 0x7f9071414230, x: 300, y: 500" 
) 
2012-03-22 19:20:49.552 ObjectCopy[18042:403] After remove last object of mutableArray2 
2012-03-22 19:20:49.552 ObjectCopy[18042:403] array1: 0x7f9071414820,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400", 
  "TestObject: 0x7f9071414230, x: 300, y: 500" 
) 
2012-03-22 19:20:49.553 ObjectCopy[18042:403] array2: 0x7f9071414820,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400", 
  "TestObject: 0x7f9071414230, x: 300, y: 500" 
) 
2012-03-22 19:20:49.553 ObjectCopy[18042:403] mutableArray2: 0x7f9072800000,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400" 
) 
2012-03-22 19:20:49.557 ObjectCopy[18042:403] mutableArray3: 0x7f90729000d0,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400" 
) 
2012-03-22 19:20:49.558 ObjectCopy[18042:403] array4: __NSArrayI, 0x7f9071416e70,  
( 
  "TestObject: 0x7f90714141b0, x: 100, y: 50", 
  "TestObject: 0x7f90714141c0, x: 200, y: 400" 
)