Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Hey again fellow Obj-C programmers. I'm on Chapter 7 of Aaron Hillegass' book, and have been trekking along just fine until this point. I can't, for the life of me, understand why there are so many steps just to add undo capabilities to an app. The author seemed to (in previous chapters) explain his steps very thoroughly, but now seems to be just winging out methods left and right.

(And NSEnumerator? He just throws it in the code without even mentioning what it does, let alone what it is!)

So I guess what I'm looking for is a better explanation of NSUndoManager and the methods you need to implement... I've been scouring the web, but have been having trouble finding any examples. Does anyone here have a tutorial-type thing they personally prefer for undo's in Cocoa? Thanks a bunch!

P.S. I'm also getting an error when I compile the RaiseMan program according to Aaron's code... It reads:
Code:
2006-07-30 21:46:28.972 RaiseMan[1356] *** -observeValueForKeyPath:ofObject:change:context: only defined for abstract class.  Define -[MyDocument observeValueForKeyPath:ofObject:change:context:]!
Any ideas on this bugger? :confused:

Thanks again everyone, for all your help. :)
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Have you defined a method -[MyDocument observeValueForKeyPath:ofObject:change:context:] as the error is asking for?

NSEnumerator lets you go through a list of elements one by one. Typically like so:
Code:
NSEnumerator *en = [someArray objectEnumerator];
id obj;
while (obj = [en nextObject]) {
  // do something with or to the object here
}
The while loop will loop through all the objects in someArray starting at the first object ([someArray reverseObjectEnumerator] would start at the last object).
 

mduser63

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
I had some trouble with the Undo stuff in Hillegass's book too, for the exact reason you describe. I don't have any specific advice. Just go back through it and carefully try to figure it all out. If it makes you feel any better, I was able to figure it out, and after some serious study, it all makes good sense. It'll probably be the same for you.

As for NSEnumerator, it sounds like you didn't go through an Objective-C book first. That might be a good idea. NSEnumerator can be pretty handy. Stephen Kochan's Programming in Objective-C is the most recommended book around here, but there are certainly others. He specifically covers NSEnumerator (along with a whole bunch of other useful stuff).
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
caveman_uk said:
Try this document for tips on undo. Undo isn't that hard - you just need to record each action you do to your data. How you do that is up to you.
Haha I actually found that article last night an hour or so after posting, seems to be really descriptive and example-ish. I like it, and have bookmarked it. :)
 

Littleodie914

macrumors 68000
Original poster
Jun 9, 2004
1,813
8
Rochester, NY
Alright, I've got one more question... In the RaiseMan example, (sorry to those who aren't familiar with this) the methods in MyDocument.m are executed when the user inserts or deletes people from the table.

Why is this? There haven't been any outlets/actions set with the "Add Person" button to run the
Code:
- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index
method... How does it know to do it automcatically?

I guess I could understand it being able to look for an insertObject method every time, but how would it expect there to be an inEmployeesAtIndex argument as well? I'm really just wondering why those methods are called, even if I haven't linked them to the buttons, or vice versa. Thanks!! :D
 

Kelmon

macrumors 6502a
Mar 28, 2005
733
0
United Kingdom
If I remember the chapter correctly the addition/removal of people from the application is performed courtesy of the ArrayController that is bound to the table and the array of employees. In this respect no IBActions are required because the ArrayController knows how to add and remove objects from the array that it is managing.

Does that help?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.