@@ -44,7 +44,7 @@ - (instancetype)init
4444 }
4545 _isMutable = YES ;
4646 _environmentState = ASEnvironmentStateMakeDefault ();
47-
47+ _children = [ NSArray array ];
4848 return self;
4949}
5050
@@ -115,24 +115,35 @@ - (void)setParent:(id<ASLayoutable>)parent
115115- (void )setChild : (id <ASLayoutable>)child
116116{
117117 ASDisplayNodeAssert (self.isMutable , @" Cannot set properties when layout spec is not mutable" );
118- ASDisplayNodeAssertNotNil (child, @" Child cannot be nil" );
119-
120- id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
121- if (finalLayoutable) {
122- _children = @[finalLayoutable];
123- [self propagateUpLayoutable: finalLayoutable];
118+ if (child) {
119+ id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
120+ if (finalLayoutable) {
121+ _children = @[finalLayoutable];
122+ [self propagateUpLayoutable: finalLayoutable];
123+ }
124+ } else {
125+ // remove the only child
126+ _children = [NSArray array ];
124127 }
125128}
126129
127130- (void )setChild : (id <ASLayoutable>)child forIdentifier : (NSString *)identifier
128131{
129132 ASDisplayNodeAssert (self.isMutable , @" Cannot set properties when layout spec is not mutable" );
130- ASDisplayNodeAssertNotNil (child, @" Child cannot be nil" );
131-
132- id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
133- self.childrenWithIdentifier [identifier] = finalLayoutable;
134- if (finalLayoutable) {
135- self.children = [self .children arrayByAddingObject: finalLayoutable];
133+ if (child) {
134+ id <ASLayoutable> finalLayoutable = [self layoutableToAddFromLayoutable: child];
135+ self.childrenWithIdentifier [identifier] = finalLayoutable;
136+ if (finalLayoutable) {
137+ _children = [_children arrayByAddingObject: finalLayoutable];
138+ }
139+ } else {
140+ id <ASLayoutable> oldChild = self.childrenWithIdentifier [identifier];
141+ if (oldChild) {
142+ self.childrenWithIdentifier [identifier] = nil ;
143+ NSMutableArray *mutableChildren = [_children mutableCopy ];
144+ [mutableChildren removeObject: oldChild];
145+ _children = [mutableChildren copy ];
146+ }
136147 }
137148
138149 // TODO: Should we propagate up the layoutable at it could happen that multiple children will propagated up their
@@ -152,6 +163,8 @@ - (void)setChildren:(NSArray<id<ASLayoutable>> *)children
152163 _children = nil ;
153164 if (finalChildren.size () > 0 ) {
154165 _children = [NSArray arrayWithObjects: &finalChildren[0 ] count: finalChildren.size ()];
166+ } else {
167+ _children = [NSArray array ];
155168 }
156169}
157170
@@ -167,7 +180,7 @@ - (void)setChildren:(NSArray<id<ASLayoutable>> *)children
167180
168181- (NSArray *)children
169182{
170- return [ _children copy ] ;
183+ return _children;
171184}
172185
173186- (void )setTraitCollection : (ASTraitCollection *)traitCollection
0 commit comments