PDA

View Full Version : Cocoa problem: What am I missing here?




Soulstorm
Aug 11, 2006, 10:23 AM
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.

http://att.macrumors.com/attachment.php?attachmentid=54813&stc=1&d=1155309759

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.



logicat2001
Aug 11, 2006, 12:01 PM
Soulstorm,

The project you uploaded is not the same as the one you're having problems with. Check it.

Best,
Anton

Soulstorm
Aug 11, 2006, 12:13 PM
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
Aug 11, 2006, 03:12 PM
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:-(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
Aug 12, 2006, 03:05 AM
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:

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:
-(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
Aug 14, 2006, 06:34 AM
I have another problem that I cannot trace with my application.

This is the problem and the steps I follow to repdroduce it:
http://att.macrumors.com/attachment.php?attachmentid=55019&stc=1&d=1155555001

-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!

[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!

slooksterPSV
Aug 15, 2006, 01:20 AM
Use CoreData - want me to link you to a great tutorial? I made two apps in less than 2 hours - 1 a recreate of MDM - MoneyData Manager, and now ADM - Assignment Data Manager. Here's a screen of ADM, then the link to the CoreData tutorial:
http://developer.apple.com/cocoa/coredatatutorial/

Oh sure, I find an error in my program just now

Soulstorm
Aug 15, 2006, 02:07 AM
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?