secret
Last active

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
@interface Test : NSObject
 
@end
 
@implementation Test
 
- (void)dealloc {
NSLog(@"Deallocated %@", self);
}
 
@end
 
 
@interface Holder : NSObject
 
@end
 
@implementation Holder {
Test *strongIvar;
__unsafe_unretained Test *unsafeIvar;
}
 
@end
 
- (void)test {
Holder *holder = [[Holder alloc] init];
NSLog(@"Setting strong");
Ivar strongIvar = class_getInstanceVariable([Holder class], "strongIvar");
object_setIvar(holder, strongIvar, [[Test alloc] init]);
 
NSLog(@"Setting unsafe");
Ivar unsafeIvar = class_getInstanceVariable([Holder class], "unsafeIvar");
object_setIvar(holder, unsafeIvar, [[Test alloc] init]);
NSLog(@"Done with %@", holder);
}

This produces the following output:

2013-03-02 13:30:41.456 TEST[44782:c07] Setting strong
2013-03-02 13:30:41.457 TEST[44782:c07] Setting unsafe
2013-03-02 13:30:41.457 TEST[44782:c07] Deallocated <Test: 0x8834fa0>
2013-03-02 13:30:43.666 TEST[44782:c07] Done with <Holder: 0x886eec0>
2013-03-02 13:30:45.044 TEST[44782:c07] Deallocated <Test: 0x7152070>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.