具体是如何NSInvocation
工作的?有好的介绍吗?
我在理解以下代码(来自Cocoa Programming for Mac OS X, 3rd Edition)如何工作时特别有问题,但随后也能够独立于教程示例应用这些概念。编码:
- (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 章,我都没有遇到任何困难。但现在 ...
提前致谢!