Objective-C cancellation manager questions

I am reading a book on Objective-c and learn about the undo manager. The concept seems very simple, but the presented example seems too complicated. Basically, I have a table view related to NSArrayController, and I add or remove people to the array, and I can edit their names and more. Since the example uses NSArrayController and bindings, the addition and deletion are automatic, and all editing is done automatically.

To use the undo manager, from what I understand, I need to implement my own methods for adding / removing / editing.

These are the methods that I implemented to create and delete and receive a call automatically because of the key value:

- (void)removeObjectFromEmployeesAtIndex:(int)index; - (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index; 

Then, for editing, I had to register the class as an observer and observe the changes for editing:

 - (void)changeKeyPath:(NSString *)keyPath ofObject:(id)obj toValue:(id)newValue 

Here are my questions:

  • Why should I do this? My understanding was that using NSArrayController and bindings was supposed to make things like adding / removing / editing elements easier and more automatic. But if I still need to implement all of these methods just by adding undo support, why use NSArrayController or bindings at all?

  • What is going on behind the scenes? In Interface Builder, the add button connects to the add method on NSArrayController. How is my insertObject method called? I know this with key value encoding, but what makes the NSArrayController add method override only b / c, does my document implement this method?

  • The solution is asymmetric. I use one concept to handle add / remove undo and another concept to handle undo changes. Can I also observe changes in the array? I guess this will complicate the observValueForKeyPath method, but will it make more sense?

+4
source share
1 answer

1) Almost, but not quite. If you think that your application code is divided into three areas: Model, View and Controller ( as described here ), then the Cocoa / XCode environment provides you with a โ€œcodelessโ€ way of handling the basics of each: IB for presentation, basic data for the model and controllers Bindings / Object for the controller.

Cancellation of control is primarily associated with the model, and not with the view or controller. So itโ€™s not Bindings or the task of an object controller to manage this stuff. It seems your problem is that you are using arrays as your data objects that are too easy to process this stuff. If you want to refuse support, you will want to use the basic data to process the model and provide these materials for free or manually create your own model objects (which will likely contain arrays) that process this logic.

FWIW, once you do this, bindings will indirectly make your life much easier, because when the cancel command returns your data to its previous state, the view will automatically reflect the changes.

In addition, the name NSArrayController is a bit misleading - it does not exist for "array management". This is true for managing data objects that are โ€œmanyโ€ related to other data objects. Which led me to ...

2) KVC allows you to relate to many relationships between an object and other objects as an array or set, regardless of how the relationships are actually implemented. It does this by requiring you to implement methods of setting up a naming convention that matches very closely the primitive methods of arrays and sets . KVC compatible objects return a proxy array or set when mutableArrayValueForKey: or mutableSetValueForKey: , which provides these methods as an array. Roughly speaking, NSArrayController knows what to call --- KVC cards between primitive array objects and some methods whose manes it generates from the key. Since you do not want to use arrays as data objects, it is usually very useful to be able to handle all-many relationships as if it were just a regular collection.

3) I think this is due to the fact that you cancel the wrong place. Introduce methods compatible with KVC to get / set properties in your data objects, make them update undoManger at the same time as setting up the data. You will need a special method when the non-non-camera will need to cancel the changes, since you do not want the cancellation to be recorded as canceled. Or you can just use Core Data and get it all for free ...

+2
source

All Articles