Question

I am having a bit of trouble removing all nodes whos name/ID is in my NSMutableArray. I have set the value for each object to be a unique name. So I can delete only the ones that are in the NSMutableArray.

The objects are created in a loop with the name of each being unique.

Like so:

    myObject.name = @"8dAN3kgh6E";

next loop

    myObject.name = @"WsFkdGrmHm"; 

next loop

    myObject.name = @"ov5BjzHGiw"; 

These values are then added and stored in an array.

NSMutableArray *currentShapeArray

(
    8dAN3kgh6E,
    WsFkdGrmHm,
    ov5BjzHGiw
)

I then loop through the currentShapeArray, which I do like so:

for (NSString *myObjectNames in currentShapeArray) {

      NSLog(@"%@", myObjectNames); //works and gives each value correctly
}

But for the life of me I cannot seem to figure out how to at this point remove the object that contains that specfic node.name.

Similar to [myObject removeFromParent]; ...but I need to select based on myObject.name property.

I am sure it is something simple, and hopefully someone can give me a nudge in the right direction. Thanks!

UPDATE: 1.

I tried what was suggested by sangony, for some reason it gives me the the following error when I use it.

2014-05-07 23:11:56.163 dotmatcher[1149:60b] NODE NAME: FY7opRB1Wk
2014-05-07 23:11:56.164 dotmatcher[1149:60b] ButtonTmp 1
2014-05-07 23:11:56.164 dotmatcher[1149:60b] Previous 1
2014-05-07 23:11:56.164 dotmatcher[1149:60b] -[__NSCFString name]: unrecognized selector    sent to instance 0xee1fe50
2014-05-07 23:11:56.167 dotmatcher[1149:60b] *** Terminating app due to uncaught     exception 'NSInvalidArgumentException', reason: '-[__NSCFString name]: unrecognized selector     sent to instance 0xee1fe50'


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {

UITouch* touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:loc];

for (SKNode *node in nodes) {

    NSString *tmp = node.name;
    if (tmp.length !=0) {
        NSLog(@"ended");

        NSString *buttonTmp = [node.userData objectForKey:@"buttonType"];

        if ([buttonTmp isEqualToString:@"1"] && [previousButton isEqualToString:@"1"]) {
            endButton = @"1";


            NSLog(@"NODE NAME: %@",node.name);
            NSLog(@"ButtonTmp %@", buttonTmp);
            NSLog(@"Previous %@", previousButton);

            NSMutableArray *discardedItems = [NSMutableArray array];
            for(SKNode *object in currentShapeArray)
            {
                if([object.name isEqualToString:node.name])
                {
                    [object removeFromParent]; // not sure if you need this or not
                    [discardedItems addObject:object];
                }
            }
            [currentShapeArray removeObjectsInArray:discardedItems];


            NSLog(@"ButtonID: %@",[node.userData objectForKey:@"buttonID"]);
            NSLog(@"ButtonType: %@",[node.userData objectForKey:@"buttonType"]);
            NSLog(@"ButtonColumn: %@",[node.userData objectForKey:@"buttonColumn"]);
            NSLog(@"ButtonRow: %@",[node.userData objectForKey:@"buttonRow"]);

            NSLog(@"AFTER REMOVE%@",currentShapeArray);

        }

    }
}
}
}

UPDATE 2 This was the final code to get it working. Thank you Sangony!

NSMutableArray *discardedItems = [NSMutableArray array];
for(SKNode *object in currentShapeArray)
{
        [object removeFromParent]; 
        [discardedItems addObject:object];

}
[currentShapeArray removeObjectsInArray:discardedItems];
Was it helpful?

Solution

Something like this perhaps:

NSMutableArray *discardedItems = [NSMutableArray array];
for(SKNode *object in currentShapeArray)
{
    if([object.name isEqualToString:node.name])
    {
        [object removeFromParent]; // not sure if you need this or not
        [discardedItems addObject:object];
    }
}
[currentShapeArray removeObjectsInArray:discardedItems];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top