mutableArrayValueForKey 和直接调用 insertObject:inEmployeesAtIndex: 之间的区别
我有一个关于使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的更新中您说:
不幸的是,无论您的
removeObjectFromEmployeesAtIndex:
方法中有什么,这都是不正确的,因为 NSMutableArray 永远不会调用您的类中的任何方法。由于您似乎正在尝试获取撤消/重做功能,因此您必须使用类似removeObjectFromEmployeesAtIndex:
的方法。否则,当您点击撤消添加员工时,您将无法“重做”添加该员工。您还可能会遇到撤消/重做对个别员工进行编辑的问题。如果您愿意,可以将removeObjectFromEmployeesAtIndex:
方法中的行[employees removeObjectAtIndex:index];
更改为[[self valueForKey:@"employees" ]removeObjectAtIndex:index];
或[self.employeesremoveObjectAtIndex:index];
但确实没有理由走这条路。In your update you say:
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 likeremoveObjectFromEmployeesAtIndex:
. 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 theremoveObjectFromEmployeesAtIndex:
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.是的。第一行(来自本书)基本上与此相同:
但是,您的代码基本上与此相同:
换句话说,您准备调用的目标在代码中是不同的(除非
self
恰好与[self mutableArrayValueForKey:@"employees"]
是同一个对象,这是值得怀疑的)。Yes. The first line (from the book) is basically equivalent to this:
Your code, however, is basically equivalent to this:
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).