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

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I am trying to create a program that will hold the program of the lessons in my university for the 7 days of the week. For each day, it will display the lessons I have in the tableView, and for each lesson, it will hold the assignments I have for the specified lesson.

attachment.php


Here is my problem, I will tell you what to do in order to reproduce it:

--for Monday, create a new lesson
--For this lesson, change the assignment by typing anything you want into the textview and then pressing the "Add Assignment" button.

Note that when you click the "Show Item Info", the info is displayed correctly (even in the console behind the program).

--Now, if you try to show the info for the newly created element, it will be displayed correctly, but when you try to display the info for the first element again, the Assignments is GONE! Just like that. It has been replaced by the assignments of the second element.

Please, can someone help me, because I have been spending hours on this, and I still can't figure out what the problem is!

Here is the entire project: http://att.macrumors.com/attachment.php?attachmentid=54824&stc=1&d=1155316382

I would appreciate any recommendation or help.

EDIT: I fixed the link so that now the correct program is uploaded.
 

Attachments

  • Picture 1.png
    Picture 1.png
    119.7 KB · Views: 697
  • cocoaki 3.zip
    104.1 KB · Views: 92

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Oh my God, I'm terribly sorry. I changed the link so that it reflects the actual program. You should be able to download it now...
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
You've got a few issues here. The fiesrt is that the line [assignmentText setString:str]; (in your showItemInfo method) was commented out, therefore nothing would display.

Also, when you're setting attributes from the GUI, you generally don't want to take the string directly because the string that the object refers to could be changed behind your back later on, and that's what appears to be happening here. Copy small strings when assigning them unless you have a good reason not to. Also, in your setter methods you want to be careful of releasing an object first because what happens when you try to assign the same object to itself (it'll happen, trust me)? You'll release the object before it can be assigned and be accessing freed memory (bad!). I've rewritten your assignment setter like this:
Code:
-(void)setLessonAssignments:(NSString *)theString {
    if (lessonAssignments != theString) {
         [lessonAssignments release];
         lessonAssignments = [theString copy];
    }
    NSLog(@"Setting the new assignments to: %@", lessonAssignments);
}
Note that I check first to make sure we're not trying to assign the same thing and do nothing if so. Also note I'm using NSLog instead of printf. If you're doing objective-C programming, NSLog is your friend, use it in most places you'd use printf (the %@ formatter code prints objects by calling their -description: method on them, which for strings is simply the string). Also note that the format string in NSLog statements must be an objective-c string (@"") or it will crash.

Finally I will say the interface is a bit goofy with having to click on the Show Item Info button to change the display (which is the same text field used to set new assignments). The whole thing as you have it now could be simplified by binding the text field to the selected row's assignment text, which would both automatically set it as you type and change the display when you click on a new row in your table.

EDIT: I took out a line that shouldn't have been in there.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Hirez, you are my hero. That sure solved my problem.

You are absolutely right the interface is goofy, I had originally intended using the method you described it. But when I saw it had problems, I tried to isolate some functions to find out what the problem was exactly.

One question about your method, though. I don't understand this statement:

HiRez said:
The whole thing as you have it now could be simplified by binding the text field to the selected row's assignment text...
Which text is that? And How can I bind it? Do you mean Cocoa Bindings? I tried changing the following function to this:
Code:
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
		   row:(int)row
{
	NSString *identifier = [tableColumn identifier];
	lesson *item = [[[Days objectAtIndex:currentDayID]dayLessons]objectAtIndex:row];
	
	[item takeValue:[assignmentText string] forKey:@"lessonAssignments"];
	[assignmentText setString: [item lessonAssignments]];
	
	return [item valueForKey:identifier];
}
but problems occured (I knew it was wrong at the time I was writting it, but a shot never hurts :) ).

Can you help me a little bit more?
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I have another problem that I cannot trace with my application.

This is the problem and the steps I follow to repdroduce it:
attachment.php


-click the '+' button to add a new lesson
-Select the new lesson and change the assignment to "Hello world" (or anything else) and click the "Add Assignment" button
-Select the initial lesson (the 'test').
-Select the newly created lesson
-Click the NSTextView and.... see the log!

Code:
[Session started at 2006-08-14 14:28:05 +0300.]
2006-08-14 14:28:05.736 cocoaki 3[426] Setting the new assignments to: Initial lesson assignment!
02006-08-14 14:28:10.066 cocoaki 3[426] Setting the new assignments to: No Assignments have been recorded yet...
2006-08-14 14:28:17.523 cocoaki 3[426] Setting the new assignments to: hello wotrld!
2006-08-14 14:28:24.297 cocoaki 3[426] -[NSBigMutableString characterAtIndex:] called with out-of-bounds index. For apps linked on Tiger this will raise an exception. For earlier apps it will produce this one-time warning and continue with existing behavior (which is undefined).
2006-08-14 14:28:24.298 cocoaki 3[426] *** -[NSBigMutableString characterAtIndex:]: Range or index out of bounds

Also, I should mention that I am facing other problems with the TextView. after I have added an assignment and try to change it by selecting the textview, the textview changes its contents to the assignment of the previewsly selected item!

Can anyone tell me hat the problem is? The project is included in my attachments... Any help would be greatly appreciated!
 

Attachments

  • Picture 1.png
    Picture 1.png
    41 KB · Views: 587
  • cocoaki 3.zip
    178.7 KB · Views: 78

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Thanks a lot. I will try CoreData, since I keep hearing everyone telling me to do so. Is CoreData really that great, I wonder...?

Anyway, thanks for the advice, but does anyone have a solution in my problem?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.