Created
April 3, 2019 09:18
-
-
Save nikolaykasyanov/5a2c1fddb7dc5cafd6ea6050128e6c81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Node: NSObject | |
@property (nonatomic, weak) Node *parent; | |
@property (nonatomic) NSArray *children; | |
@end | |
@implementation Node { | |
NSMutableArray *_children; | |
} | |
- init { | |
self = [super init]; | |
_children = [NSMutableArray array]; | |
return self; | |
} | |
- (void)dealloc { | |
NSLog(@"Node.dealloc"); | |
} | |
- (void)addChild:(Node *)child { | |
child->_parent = self; | |
[_children addObject:child]; | |
} | |
- (void)removeFromParent { | |
NSLog(@"Gonna remove myself from parent..."); | |
[_parent->_children removeObject:self]; | |
_parent = nil; | |
NSLog(@"Removed myself from parent"); | |
} | |
@end | |
static Node *setup() { | |
__auto_type root = [Node new]; | |
[root addChild:[Node new]]; | |
return root; | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
__auto_type root = setup(); | |
__unsafe_unretained Node *child = root.children[0]; | |
[child removeFromParent]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment