我在删除名称/ ID在我的NSMutableArray中的所有节点时遇到了一些麻烦。 我已将每个对象的值设置为唯一名称。所以我只能删除NSMutableArray中的那些。
对象是在循环中创建的,每个对象的名称都是唯一的。
像这样:
myObject.name = @"8dAN3kgh6E";
下一个循环
myObject.name = @"WsFkdGrmHm";
下一个循环
myObject.name = @"ov5BjzHGiw";
然后添加这些值并将其存储在数组中。
NSMutableArray * currentShapeArray
(
8dAN3kgh6E,
WsFkdGrmHm,
ov5BjzHGiw
)
然后我遍历currentShapeArray,我喜欢这样:
for (NSString *myObjectNames in currentShapeArray) {
NSLog(@"%@", myObjectNames); //works and gives each value correctly
}
但是对于我的生活,我似乎无法弄清楚如何删除包含那个specfic node.name的对象。
与[myObject removeFromParent]类似; ...但我需要根据myObject.name属性进行选择。
我确信这很简单,希望有人可以给我一个正确方向的推动。谢谢!
更新:1。
我尝试过sangony的建议,出于某种原因,当我使用它时,它会给我以下错误。
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);
}
}
}
}
}
更新2 这是让它运转的最终代码。谢谢Sangony!
NSMutableArray *discardedItems = [NSMutableArray array];
for(SKNode *object in currentShapeArray)
{
[object removeFromParent];
[discardedItems addObject:object];
}
[currentShapeArray removeObjectsInArray:discardedItems];
答案 0 :(得分:2)
或许这样的事情:
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];