Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % cat testARC.m
- #import <Foundation/Foundation.h>
- @interface CopyMe : NSObject<NSCopying>
- @end
- @implementation CopyMe
- - (instancetype)init
- {
- NSLog(@"%s", __func__);
- return [super init];
- }
- - (instancetype)copyWithZone:(NSZone *)zone
- {
- NSLog(@"%s", __func__);
- // Immutable type; just return as-is.
- return self;
- }
- - (void)dealloc
- {
- NSLog(@"%s", __func__);
- }
- @end
- @interface RetainMe : NSObject
- @end
- @implementation RetainMe
- - (instancetype)init
- {
- NSLog(@"%s", __func__);
- return [super init];
- }
- - (void)dealloc
- {
- NSLog(@"%s", __func__);
- }
- @end
- @interface Copier : NSObject
- @property (nonatomic, copy, readwrite) CopyMe *copied;
- @property (nonatomic, strong, readwrite) RetainMe *retained;
- @end
- @implementation Copier
- - (instancetype)initWithCopyMeExplicitCopy:(CopyMe *)copied
- {
- NSLog(@"%s", __func__);
- if ((self = [super init])) {
- _copied = [copied copy];
- }
- return self;
- }
- - (instancetype)initWithCopyMePropertyAssign:(CopyMe *)copied
- {
- NSLog(@"%s", __func__);
- if ((self = [super init])) {
- self.copied = copied;
- }
- return self;
- }
- - (instancetype)initWithCopyMeMemberAssign:(CopyMe *)copied
- {
- NSLog(@"%s", __func__);
- if ((self = [super init])) {
- _copied = copied;
- }
- return self;
- }
- - (instancetype)initWithRetainMePropertyAssign:(RetainMe *)retained
- {
- NSLog(@"%s", __func__);
- if ((self = [super init])) {
- self.retained = retained;
- }
- return self;
- }
- - (instancetype)initWithRetainMeMemberAssign:(RetainMe *)retained
- {
- NSLog(@"%s", __func__);
- if ((self = [super init])) {
- _retained = retained;
- }
- return self;
- }
- - (void)dealloc
- {
- NSLog(@"%s", __func__);
- }
- @end
- int main(int argc, char **argv)
- {
- Copier *copier;
- NSLog(@"=== TEST EXPLICIT COPY ===");
- copier = [[Copier alloc] initWithCopyMeExplicitCopy:[[CopyMe alloc] init]];
- copier = nil;
- NSLog(@"=== TEST COPY @PROPERTY PROPERTY ASSIGN ===");
- copier = [[Copier alloc] initWithCopyMePropertyAssign:[[CopyMe alloc] init]];
- copier = nil;
- NSLog(@"=== TEST COPY @PROPERTY MEMBER ASSIGN ===");
- copier = [[Copier alloc] initWithCopyMeMemberAssign:[[CopyMe alloc] init]];
- copier = nil;
- NSLog(@"=== TEST STRONG @PROPERTY PROPERTY ASSIGN ===");
- copier = [[Copier alloc] initWithRetainMePropertyAssign:[[RetainMe alloc] init]];
- copier = nil;
- NSLog(@"=== TEST STRONG @PROPERTY MEMBER ASSIGN ===");
- copier = [[Copier alloc] initWithRetainMeMemberAssign:[[RetainMe alloc] init]];
- copier = nil;
- NSLog(@"=== DONE ===");
- }
- % clang -Wall -fobjc-arc -o testARC testARC.m
- % ./testARC
- 2013-09-15 12:13:05.320 testARC[53792:707] === TEST EXPLICIT COPY ===
- 2013-09-15 12:13:05.322 testARC[53792:707] -[CopyMe init]
- 2013-09-15 12:13:05.323 testARC[53792:707] -[Copier initWithCopyMeExplicitCopy:]
- 2013-09-15 12:13:05.323 testARC[53792:707] -[CopyMe copyWithZone:]
- 2013-09-15 12:13:05.324 testARC[53792:707] -[Copier dealloc]
- 2013-09-15 12:13:05.325 testARC[53792:707] -[CopyMe dealloc]
- 2013-09-15 12:13:05.325 testARC[53792:707] === TEST COPY @PROPERTY PROPERTY ASSIGN ===
- 2013-09-15 12:13:05.326 testARC[53792:707] -[CopyMe init]
- 2013-09-15 12:13:05.326 testARC[53792:707] -[Copier initWithCopyMePropertyAssign:]
- 2013-09-15 12:13:05.327 testARC[53792:707] -[CopyMe copyWithZone:]
- 2013-09-15 12:13:05.328 testARC[53792:707] -[Copier dealloc]
- 2013-09-15 12:13:05.328 testARC[53792:707] -[CopyMe dealloc]
- 2013-09-15 12:13:05.329 testARC[53792:707] === TEST COPY @PROPERTY MEMBER ASSIGN ===
- 2013-09-15 12:13:05.329 testARC[53792:707] -[CopyMe init]
- 2013-09-15 12:13:05.330 testARC[53792:707] -[Copier initWithCopyMeMemberAssign:]
- 2013-09-15 12:13:05.332 testARC[53792:707] -[Copier dealloc]
- 2013-09-15 12:13:05.332 testARC[53792:707] -[CopyMe dealloc]
- 2013-09-15 12:13:05.334 testARC[53792:707] === TEST STRONG @PROPERTY PROPERTY ASSIGN ===
- 2013-09-15 12:13:05.335 testARC[53792:707] -[RetainMe init]
- 2013-09-15 12:13:05.336 testARC[53792:707] -[Copier initWithRetainMePropertyAssign:]
- 2013-09-15 12:13:05.337 testARC[53792:707] -[Copier dealloc]
- 2013-09-15 12:13:05.338 testARC[53792:707] -[RetainMe dealloc]
- 2013-09-15 12:13:05.339 testARC[53792:707] === TEST STRONG @PROPERTY MEMBER ASSIGN ===
- 2013-09-15 12:13:05.339 testARC[53792:707] -[RetainMe init]
- 2013-09-15 12:13:05.339 testARC[53792:707] -[Copier initWithRetainMeMemberAssign:]
- 2013-09-15 12:13:05.340 testARC[53792:707] -[Copier dealloc]
- 2013-09-15 12:13:05.340 testARC[53792:707] -[RetainMe dealloc]
- 2013-09-15 12:13:05.340 testARC[53792:707] === DONE ===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement