Advertisement
beng

Untitled

Sep 15th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % cat testARC.m
  2. #import <Foundation/Foundation.h>
  3.  
  4. @interface CopyMe : NSObject<NSCopying>
  5.  
  6. @end
  7.  
  8. @implementation CopyMe
  9.  
  10. - (instancetype)init
  11. {
  12.   NSLog(@"%s", __func__);
  13.   return [super init];
  14. }
  15.  
  16. - (instancetype)copyWithZone:(NSZone *)zone
  17. {
  18.   NSLog(@"%s", __func__);
  19.   // Immutable type; just return as-is.
  20.   return self;
  21. }
  22.  
  23. - (void)dealloc
  24. {
  25.   NSLog(@"%s", __func__);
  26. }
  27.  
  28. @end
  29.  
  30. @interface RetainMe : NSObject
  31.  
  32. @end
  33.  
  34. @implementation RetainMe
  35.  
  36. - (instancetype)init
  37. {
  38.   NSLog(@"%s", __func__);
  39.   return [super init];
  40. }
  41.  
  42. - (void)dealloc
  43. {
  44.   NSLog(@"%s", __func__);
  45. }
  46.  
  47. @end
  48.  
  49. @interface Copier : NSObject
  50.  
  51. @property (nonatomic, copy, readwrite) CopyMe *copied;
  52. @property (nonatomic, strong, readwrite) RetainMe *retained;
  53.  
  54. @end
  55.  
  56. @implementation Copier
  57.  
  58. - (instancetype)initWithCopyMeExplicitCopy:(CopyMe *)copied
  59. {
  60.   NSLog(@"%s", __func__);
  61.   if ((self = [super init])) {
  62.     _copied = [copied copy];
  63.   }
  64.   return self;
  65. }
  66.  
  67. - (instancetype)initWithCopyMePropertyAssign:(CopyMe *)copied
  68. {
  69.   NSLog(@"%s", __func__);
  70.   if ((self = [super init])) {
  71.     self.copied = copied;
  72.   }
  73.   return self;
  74. }
  75.  
  76. - (instancetype)initWithCopyMeMemberAssign:(CopyMe *)copied
  77. {
  78.   NSLog(@"%s", __func__);
  79.   if ((self = [super init])) {
  80.     _copied = copied;
  81.   }
  82.   return self;
  83. }
  84.  
  85. - (instancetype)initWithRetainMePropertyAssign:(RetainMe *)retained
  86. {
  87.   NSLog(@"%s", __func__);
  88.   if ((self = [super init])) {
  89.     self.retained = retained;
  90.   }
  91.   return self;
  92. }
  93.  
  94. - (instancetype)initWithRetainMeMemberAssign:(RetainMe *)retained
  95. {
  96.   NSLog(@"%s", __func__);
  97.   if ((self = [super init])) {
  98.     _retained = retained;
  99.   }
  100.   return self;
  101. }
  102.  
  103. - (void)dealloc
  104. {
  105.   NSLog(@"%s", __func__);
  106. }
  107.  
  108. @end
  109.  
  110. int main(int argc, char **argv)
  111. {
  112.   Copier *copier;
  113.   NSLog(@"=== TEST EXPLICIT COPY ===");
  114.   copier = [[Copier alloc] initWithCopyMeExplicitCopy:[[CopyMe alloc] init]];
  115.   copier = nil;
  116.  
  117.   NSLog(@"=== TEST COPY @PROPERTY PROPERTY ASSIGN ===");
  118.   copier = [[Copier alloc] initWithCopyMePropertyAssign:[[CopyMe alloc] init]];
  119.   copier = nil;
  120.  
  121.   NSLog(@"=== TEST COPY @PROPERTY MEMBER ASSIGN ===");
  122.   copier = [[Copier alloc] initWithCopyMeMemberAssign:[[CopyMe alloc] init]];
  123.   copier = nil;
  124.  
  125.   NSLog(@"=== TEST STRONG @PROPERTY PROPERTY ASSIGN ===");
  126.   copier = [[Copier alloc] initWithRetainMePropertyAssign:[[RetainMe alloc] init]];
  127.   copier = nil;
  128.  
  129.   NSLog(@"=== TEST STRONG @PROPERTY MEMBER ASSIGN ===");
  130.   copier = [[Copier alloc] initWithRetainMeMemberAssign:[[RetainMe alloc] init]];
  131.   copier = nil;
  132.  
  133.   NSLog(@"=== DONE ===");
  134. }
  135.  
  136. % clang -Wall -fobjc-arc -o testARC testARC.m
  137.  
  138. % ./testARC                                  
  139. 2013-09-15 12:13:05.320 testARC[53792:707] === TEST EXPLICIT COPY ===
  140. 2013-09-15 12:13:05.322 testARC[53792:707] -[CopyMe init]
  141. 2013-09-15 12:13:05.323 testARC[53792:707] -[Copier initWithCopyMeExplicitCopy:]
  142. 2013-09-15 12:13:05.323 testARC[53792:707] -[CopyMe copyWithZone:]
  143. 2013-09-15 12:13:05.324 testARC[53792:707] -[Copier dealloc]
  144. 2013-09-15 12:13:05.325 testARC[53792:707] -[CopyMe dealloc]
  145. 2013-09-15 12:13:05.325 testARC[53792:707] === TEST COPY @PROPERTY PROPERTY ASSIGN ===
  146. 2013-09-15 12:13:05.326 testARC[53792:707] -[CopyMe init]
  147. 2013-09-15 12:13:05.326 testARC[53792:707] -[Copier initWithCopyMePropertyAssign:]
  148. 2013-09-15 12:13:05.327 testARC[53792:707] -[CopyMe copyWithZone:]
  149. 2013-09-15 12:13:05.328 testARC[53792:707] -[Copier dealloc]
  150. 2013-09-15 12:13:05.328 testARC[53792:707] -[CopyMe dealloc]
  151. 2013-09-15 12:13:05.329 testARC[53792:707] === TEST COPY @PROPERTY MEMBER ASSIGN ===
  152. 2013-09-15 12:13:05.329 testARC[53792:707] -[CopyMe init]
  153. 2013-09-15 12:13:05.330 testARC[53792:707] -[Copier initWithCopyMeMemberAssign:]
  154. 2013-09-15 12:13:05.332 testARC[53792:707] -[Copier dealloc]
  155. 2013-09-15 12:13:05.332 testARC[53792:707] -[CopyMe dealloc]
  156. 2013-09-15 12:13:05.334 testARC[53792:707] === TEST STRONG @PROPERTY PROPERTY ASSIGN ===
  157. 2013-09-15 12:13:05.335 testARC[53792:707] -[RetainMe init]
  158. 2013-09-15 12:13:05.336 testARC[53792:707] -[Copier initWithRetainMePropertyAssign:]
  159. 2013-09-15 12:13:05.337 testARC[53792:707] -[Copier dealloc]
  160. 2013-09-15 12:13:05.338 testARC[53792:707] -[RetainMe dealloc]
  161. 2013-09-15 12:13:05.339 testARC[53792:707] === TEST STRONG @PROPERTY MEMBER ASSIGN ===
  162. 2013-09-15 12:13:05.339 testARC[53792:707] -[RetainMe init]
  163. 2013-09-15 12:13:05.339 testARC[53792:707] -[Copier initWithRetainMeMemberAssign:]
  164. 2013-09-15 12:13:05.340 testARC[53792:707] -[Copier dealloc]
  165. 2013-09-15 12:13:05.340 testARC[53792:707] -[RetainMe dealloc]
  166. 2013-09-15 12:13:05.340 testARC[53792:707] === DONE ===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement