2

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];
4
  • it would be a lot simpler to just store the nodes in the array, or even better if you can add them all to the same node and no other nodes to it so that you can use that node's children array as your storage and be able to use enumerateChildNodes... Commented May 8, 2014 at 6:21
  • @LearnCocos2D what are the advantages of doing it that way instead of the way mentioned by sangony? Commented May 10, 2014 at 17:36
  • Because you get the "get by name" and enumeration with name for free, and no extra storage needed, no extra code to add/remove nodes to a custom array in order to keep it in sync, and thus fewer potential coding bugs. Commented May 10, 2014 at 17:41
  • @LearnCocos2D I see.... looks like I have a small part to recode :) Thanks for the tip. Commented May 11, 2014 at 12:13

1 Answer 1

2

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];
Sign up to request clarification or add additional context in comments.

9 Comments

thank you for the help, I have tried what you suggested, but Now it is crashing on me. I have updated my question please have a look if you have the time. Thank you.
@user3450888 - In the line if([object.name isEqualToString:node.name]), did you modify it to suit your needs? In other words, what's the object you want to remove from the array?
I am trying to remove any node(s) from its parent(column1/column2 etc) that has the same "node.name" that is found within the NSMutableArray currentShapeArray. So wouldn't object be this, or am I mistaken?
@user3450888 - I suggest you check the array's content. Put a NSLog(@"%@", currentShapeArray); right after the NSLog(@"Previous %@", previousButton); line so you can see the array content.
I did that, it gives me the list of node.name's that I saved to the array each as a NSString.... Do I need to save the SKNode to the array instead? I am still not sure why it is causing it to crash.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.