我希望在一段时间后从NSMutableDictionary中删除一些对象。
我有:
//first Method start
PedObjectTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fireTimer:) userInfo:PedMessage repeats:YES];
[PedMessage setObject:PedObjectTimer forKey:@"Timer"];
[PedObjects setObject:[PedMessage copy] forKey:[PedMessage objectForKey:@"ObjectID"]];
//first Method end
- (void) fireTimer:(NSTimer*)theTimer {
NSLog(@"%@",PedObjects);
[PedObjects removeObjectForKey:[[theTimer userInfo]objectForKey:@"ObjectID"]];
NSLog(@"%@",PedObjects);
[theTimer invalidate];
theTimer = nil;
}
问题是我无法将Timer对象复制到字典中......
如何在fireTimer方法中2秒后删除正确的一个?
用这个我得到相同的结果,如果我有2个选择器它不会删除我的字典中的2个条目......
[PedObjects setObject:[PedMessage copy] forKey:[PedMessage objectForKey:@"ObjectID"]];
[self performSelector:@selector(delete:) withObject:[PedObjects objectForKey:[PedMessage objectForKey:@"ObjectID"]] afterDelay:2.0];
-(void)delete:(NSMutableDictionary*)dict
{
NSLog(@"%@",[PedMessage objectForKey:@"ObjectID"]);
NSLog(@"%@",PedObjects);
[PedObjects removeObjectForKey:[dict objectForKey:@"ObjectID"]];
NSLog(@"%@",PedObjects);
}
答案 0 :(得分:5)
您可以使用以下内容:
而不是计时器,你打电话
[self performSelector:@selector(delete:) withObject:yourDictionary afterDelay:2.0];
//delete implementation
-(void)delete:(NSMutableDictionary*)dict {
[dict removeObjectForKey:@"yourKey"];
}