ObjC:将逻辑放入一个变量中,并将该变量排列在一个数组中...白日梦? [英] ObjC: Put logic within a variable, and queue that variable in an array... daydreaming?
问题描述
我试图将下面的JS代码翻译成ObjC;它基本上定义了一个函数并将其存储在一个数组中供以后执行:
I'm trying to translate the following JS code to ObjC; It basically defines a function and store it in an array for later execution:
var logic = function() {
me.removeNode(node);
}
this.queue.push(logic);
我的ObjC端口包含以下方法: - (void)removeNode :( AbstractNode *)node
并且我卡住了...我想用一个指定的node参数来调用这个方法...
My ObjC port contains the method: -(void)removeNode:(AbstractNode*)node
and I'm stuck... I would like to just queue a call to this method using the specified "node" argument...
编辑:我正在阅读有关选择器。这仍然是模糊的,但可能会有所帮助。不知何故。或者不是?
edit: I'm reading about selectors. It's still blurry, but might help. Somehow. Or not?
是否有可能,或者我需要找到解决方法:-)?
Thanks!
Is it even possible, or will I need to find a workaround :-) ? Thanks!
解决方案要感谢社区:
Solution to this thanks to the community:
// Queue declaration
NSMutableArray *IRQ = [[NSMutableArray array] retain];
// Add logic to the queue for delayed execution:
[IRQ addObject:^{ NSLog(@"thanks for the help!"); } ];
// Call later
for (int i = 0; i < [IRQ count]; i++){
void (^delayedCall)(void) = [IRQ objectAtIndex:i];
delayedCall();
}
推荐答案
p>
Use a block:
[queue addObject:^{
[me removeNode:node];
}];
以下是对块的简短介绍。它们是匿名函数的Objective-C等价物。
Here's a short introduction to blocks. They are the Objective-C equivalent of anonymous functions.
这篇关于ObjC:将逻辑放入一个变量中,并将该变量排列在一个数组中...白日梦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!