Skip to content

Instantly share code, notes, and snippets.

@nikolaykasyanov
Created April 3, 2019 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikolaykasyanov/5a2c1fddb7dc5cafd6ea6050128e6c81 to your computer and use it in GitHub Desktop.
Save nikolaykasyanov/5a2c1fddb7dc5cafd6ea6050128e6c81 to your computer and use it in GitHub Desktop.
#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