NSInvocation
工作原理如何?有没有很好的介绍?
我在理解以下代码(来自Mac OS X的Cocoa编程,第3版)如何工作时遇到一些问题,但同时也能够独立于教程示例来应用这些概念。代码:
- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index
{
NSLog(@"adding %@ to %@", p, employees);
// Add inverse of this operation to undo stack
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
if (![undo isUndoing])
[undo setActionName:@"Insert Person"];
// Finally, add person to the array
[employees insertObject:p atIndex:index];
}
- (void)removeObjectFromEmployeesAtIndex:(int)index
{
Person *p = [employees objectAtIndex:index];
NSLog(@"removing %@ from %@", p, employees);
// Add inverse of this operation to undo stack
NSUndoManager *undo = [self undoManager];
[[undo prepareWithInvocationTarget:self] insertObject:p
inEmployeesAtIndex:index];
if (![undo isUndoing])
[undo setActionName:@"Delete Person"];
// Finally, remove person from array
[employees removeObjectAtIndex:index];
}
我知道它正在尝试做什么。(顺便说一句,employees
是NSArray
自定义Person
类的。)
作为.NET的人,我尝试将不熟悉的Obj-C和Cocoa概念与大致类似的.NET概念相关联。这是否类似于.NET的委托概念,但未键入?
从书中这还不是100%清楚的,所以我正在寻找真正的Cocoa / Obj-C专家的补充,其目的是让我理解简单(-ish)示例下的基本概念。我真的希望能够独立地应用这些知识-直到第9章,我都没有遇到任何困难。但现在 ...
提前致谢!
setArgument:atIndex:
,因此arg赋值实际上应该为[myInvocation setArgument:&myString atIndex:2]
。