mutableArrayValueForKey 和直接调用 insertObject:inEmployeesAtIndex: 之间的区别

发布于 2024-09-05 08:14:12 字数 820 浏览 5 评论 0原文

我有一个关于使用 KVO 兼容方法从数组中插入/删除对象的问题。我正在研究 Aaron Hillegass 的 Mac OS X Cocoa 编程,我看到了以下代码行(在 insertObject:inEmployeesAtIndex: 方法中:

[[undoManager prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];

如果我错了,请纠正我,但我一直认为最好先调用 mutableArrayValueForKey:,然后调用 removeObjectAtIndex:...所以我尝试将上面的行更改为:

[[undoManager prepareWithInvocationTarget:[self mutableArrayValueForKey:@"employees"]] removeObjectAtIndex:index]; 

但它不起作用,有人可以解释一下区别吗?为什么第一行有效,但第二行无效?

更新:我的removeObjectFromEmployeesAtIndex:index方法的实现是为了使我的集合类(NSMutableArray的实例)符合KVC标准,所以最终调用[[self mutableArrayValueForKey: @"employees"]removeObjectAtIndex:index]; 最终应该调用 [self removeObjectFromEmployeesAtIndex:index];

I have a question regarding using KVO-compliant methods to insert/remove objects from an array. I'm working through Aaron Hillegass' Cocoa Programming for Mac OS X and I saw the following line of code (in the insertObject:inEmployeesAtIndex: method:

[[undoManager prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];

Correct me if I'm wrong, but I always thought it was better to call mutableArrayValueForKey: and then removeObjectAtIndex:...so I tried changing the above line to this:

[[undoManager prepareWithInvocationTarget:[self mutableArrayValueForKey:@"employees"]] removeObjectAtIndex:index]; 

And it didn't work. Can someone explain the difference and why the first line works but the second line doesn't?

UPDATE: My removeObjectFromEmployeesAtIndex:index method is implemented to make my collection class (an instance of NSMutableArray) KVC-compliant. So ultimately, calling [[self mutableArrayValueForKey:@"employees"] removeObjectAtIndex:index]; should end up calling [self removeObjectFromEmployeesAtIndex:index];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-09-12 08:14:12

在您的更新中您说:

调用 [[self mutableArrayValueForKey:@"employees"]removeObjectAtIndex:index];最终应该调用 [self removeObjectFromEmployeesAtIndex:index];

不幸的是,无论您的 removeObjectFromEmployeesAtIndex: 方法中有什么,这都是不正确的,因为 NSMutableArray 永远不会调用您的类中的任何方法。由于您似乎正在尝试获取撤消/重做功能,因此您必须使用类似 removeObjectFromEmployeesAtIndex: 的方法。否则,当您点击撤消添加员工时,您将无法“重做”添加该员工。您还可能会遇到撤消/重做对个别员工进行编辑的问题。如果您愿意,可以将 removeObjectFromEmployeesAtIndex: 方法中的行 [employees removeObjectAtIndex:index]; 更改为 [[self valueForKey:@"employees" ]removeObjectAtIndex:index];[self.employeesremoveObjectAtIndex:index]; 但确实没有理由走这条路。

In your update you say:

calling [[self mutableArrayValueForKey:@"employees"] removeObjectAtIndex:index]; should end up calling [self removeObjectFromEmployeesAtIndex:index];

Unfortunately this is not correct not matter what is in your removeObjectFromEmployeesAtIndex: method as NSMutableArray will never call any methods in your class. Since you seem to be trying to get undo/redo functionality you have to use a method like removeObjectFromEmployeesAtIndex:. Otherwise when you hit undo for adding an employee you will have no way to 'redo' adding that employee. You also could have issues with undo/redo for edits to individual employees. If you wanted to you could change the line in the removeObjectFromEmployeesAtIndex: method that reads [employees removeObjectAtIndex:index]; to [[self valueForKey:@"employees"] removeObjectAtIndex:index]; or [self.employees removeObjectAtIndex:index]; but there is really no reason to go this route.

本宫微胖 2024-09-12 08:14:12

是的。第一行(来自本书)基本上与此相同:

id tmp = [undoManager prepareWithInvocationTarget:self];
[tmp removeObejctFromEmployeesAtIndex:index];

但是,您的代码基本上与此相同:

id tmp1 = [self mutableArrayValueForKey:@"employees"];
id tmp2 = [undoManager prepareWithInvocationTarget:tmp1];
[tmp2 removeObjectAtIndex:index];

换句话说,您准备调用的目标在代码中是不同的(除非 self 恰好与 [self mutableArrayValueForKey:@"employees"] 是同一个对象,这是值得怀疑的)。

Yes. The first line (from the book) is basically equivalent to this:

id tmp = [undoManager prepareWithInvocationTarget:self];
[tmp removeObejctFromEmployeesAtIndex:index];

Your code, however, is basically equivalent to this:

id tmp1 = [self mutableArrayValueForKey:@"employees"];
id tmp2 = [undoManager prepareWithInvocationTarget:tmp1];
[tmp2 removeObjectAtIndex:index];

In other words, the target that you're preparing the invocation with is different in your code (unless self happens to be the same object as [self mutableArrayValueForKey:@"employees"], which is doubtful).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文