Trouble with Hillgass book's examples not working

Discussion in 'Mac Programming' started by LoveMyMac2004, Aug 23, 2008.

  1. macrumors newbie

    Aug 18, 2008
    Myrtle Beach, SC
    Hi all!

    Well I am in Chapter 9 and the code entered is not working. I am thinking that this must be another Objective-C 2.0 example and I only have Objective C 1.0.

    Here is the method:

    - (IBAction)createEmployee:(id)sender
    	NSWindow *w = [tableView window];
    	// Try to end any editing that is taking place
    	BOOL editingEnded = [w makeFirstResponder:w];
    	if (!editingEnded)
    		NSLog(@"Unable to end editing");
    	NSUndoManager *undo = [self undoManager];
    	// Has an edit occured already in this event?
    	if ([undo groupingLevel])
    		// Close the last group
    		[undo endUndoGrouping];
    		// Open a new group
    		[undo beginUndoGrouping];
    	// Create the object
    	Person *p = [employeeController newObject];
    	// Add it to the content array of 'employeeController'
    	[employeeController addObject:p];
    	[p release];
    	// Re-sort (in case the user has sorted a column)
    	[employeeController rearrangeObjects];
    	// Get the sorted array
    	NSArray *a = [employeeController arrangedObjects];
    	// Find the object just added
    	int row = [a indexOfObjectIdenticalTo:p];
    	NSLog(@"starting edit of %@ in row %d", p, row);
    	// Begin the edit in the first column
    	[tableView editColumn:0 
    Now here is the problem, in the Create the object part: employeeController is defined in the header file as NSArrayController. However at this point it is nil. (I just added a test before it create p). Where should the connection be made for employeeController.

    Here is the header:

    #import <Cocoa/Cocoa.h>
    @class Person;
    @interface MyDocument : NSDocument
    	NSMutableArray *employees;
    	IBOutlet NSTableView *tableView;
    	IBOutlet NSArrayController *employeeController;
    - (IBAction)createEmployee:(id)sender;
    So at the moment I am stuck in the book. I thank you all in advance for the help. You guys (or gals) are great and I thank you for your patience.

  2. macrumors 601


    Jan 6, 2004
    Western US
    NSTableView inherits from NSControl, which in turn inherits from NSView, which does have a window method (which should be the window it's contained in). I'd check your connections in Interface Builder. It may be that you have not connected the tableView outlet?

    I believe NO is equivalent to 0 and YES is equivalent to 1 (and 1 only, not any non-zero number).

    NSArrayController inherits from NSObjectController, which does have a newObject method.

    In general, I wouldn't worry too much about Objective-C 1.0 vs 2.0, they aren't very different for most things. 2.0 added only a few conveniences, most of which Hillegass doesn't use much in the book anyway.
  3. thread starter macrumors newbie

    Aug 18, 2008
    Myrtle Beach, SC
    I feel like an idiot!

    I figured out all my problems. I tried several things over the last few days and nothing seemed to work.

    It was because the changes to the header file were not 'seen' in Interface Builder. I had to go to the class for MyDocument and read the header file again. It warned me about losing the connections and did I want to continue. I continued and Viola! It all worked!

    That was a valuable lesson. I hope this helps anyone else.

    I do have a question tho, if you make changes in the header file in XCode 3.x does the new IB 'see' the changes automatically? Not that it matters to me since I am using XCode 2.5, Tiger, and Obj-C 1.0. Just wondered if Apple "fixed" it in the newer version.

    Thanks again!

  4. macrumors 68030


    Sep 13, 2001
    Portland, OR
    Yup, they did. A very welcome improvement :)
  5. macrumors 65816


    Jul 5, 2007
    If you are referring to IBAction's and Outlets in the header then yes. You must save the header file in xCode 3.x first. After that when you open the NIB (.XIB in xcode 3) in IB the changes will appear. However if you rename an Action or outlet that was already connected in IB, the IB will not update to the renamed connection. It will still show the old connection, even if the item no longer exists in the header file. It will indicate an issue by changing the color of the receiving objects name to yellow when you right click on it.

Share This Page