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

LoveMyMac2004

macrumors newbie
Original poster
Aug 18, 2008
13
0
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:

Code:
- (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");
		return;
	}
	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 
					  row:row 
				withEvent:nil 
				   select:YES];
}

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:

Code:
#import <Cocoa/Cocoa.h>
@class Person;

@interface MyDocument : NSDocument
{
	NSMutableArray *employees;
	IBOutlet NSTableView *tableView;
	IBOutlet NSArrayController *employeeController;
}
- (IBAction)createEmployee:(id)sender;
@end

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.

-Don
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
1. w is returning nil which I believe is what is causing the method to return. I cannot find a method in the NSTableView docs. It appears that it should return the window object for the NSTableView object. If this is an Obj-C 2.0 method, how can I do the same thing in Obj-C 1.0
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?

2. assuming that I either comment out the code for point 1, the if statement after creation of the undo object is checking [undo groupingLevel. According to the Apple docs, this returns an int and not a BOOL. However am I correct in assuming that the if will evaluate a YES if the int is non-zero as it does in C? I think I can change it to if([undo groupingLevel] != 0)... to get the same result. Not a big problem I guess.
I believe NO is equivalent to 0 and YES is equivalent to 1 (and 1 only, not any non-zero number).

3. the Create the object part: employeeController is defined in the header file as NSArrayController. Looking in the Apple docs, there is no 'newObject' method defined. I am getting nil for p. So the same question as in 1. If it is an Obj-c 2.0 what would be the Obj-C 1.0 equivalent.
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.
 

LoveMyMac2004

macrumors newbie
Original poster
Aug 18, 2008
13
0
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!

-Don
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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.

Yup, they did. A very welcome improvement :)
 

gehrbox

macrumors 65816
Jul 5, 2007
1,040
0
Charleston,SC
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.