Re: newbie question
Re: newbie question
- Subject: Re: newbie question
- From: daniel <email@hidden>
- Date: Sat, 13 Nov 2004 11:32:31 -0800
Hi Daniel - at a quick glance I see some serious memory problems in your code. It may or may not ultimately be the cause of your problems, but you should fix them:
You are allocating empty strings in your init routine (whatUserTyped, and theLength). Later, in countLetters and startOver, you are replacing those allocated empty strings with new, autoreleased strings. These strings will be invalid as soon as your runloop gets a chance to dispose of them.
You should look into the concept of using "accessor" functions to manage your instance variables. That will be a good lesson in getting your code into working order. Read this article:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
Daniel
On Nov 13, 2004, at 12:30 AM, Daniel Child wrote:
Hi,
I am trying to put together a (very basic) challenge problem in Hillegas's book, but it is not running properly. Basically, I have an NSTextField object in IB which is supposed to receive a string. When you press the countLettersButton, the program is supposed to count the letters in the String. Sadly, I can't get that much to work, though I can reset fields to empty and 0 using the StartOver button I created. Here the critical parts of code.
@interface MyDocument : NSDocument
{
// INSTANCE VARIABLES
int numLetters;
NSString *whatUserTyped;
NSString *theLength;
// IBOUTLETS
IBOutlet NSButton *countLettersButton;
IBOutlet NSButton *startOverButton;
IBOutlet NSTextField *theStringToCount; // where user inputs a string
IBOutlet NSTextField *theAnswer; // where the answer comes back
}
// ACTION METHODS
- (id)sender;
- (id)sender;
// PRIVATE METHODS
- (void)updateUI;
@end
And the rest of it is as follows:
- (id)init
{
self = [super init];
if (self)
{
whatUserTyped = [[NSString alloc] init];
theLength = [[NSString alloc] init];
numLetters = 0;
}
return self;
}
- (id)sender
{
whatUserTyped = [theStringToCount stringValue];
numLetters = [whatUserTyped length];
theLength = [NSString stringWithFormat: @"%d", numLetters];
[self updateUI];
}
- (id)sender
{
whatUserTyped = @"";
numLetters = 0;
theLength = [NSString stringWithFormat: @"%d", numLetters];
[self updateUI];
}
- (void)updateUI
{
[theStringToCount setStringValue: whatUserTyped];
[theAnswer setStringValue: theLength];
}
(There are some automatically included methods that I'm leaving out since they are standard.) The interface looks OK, but when I click on countLettersButton, the answer field is set to 0. At the same time, the cursor in the entry field indicates that there are invisible letters there, and if you highlight them, the original letters reappear, but only if highlighted. Clearly, something is wrong with the countLetters method, as well as with something else which messes up the display. I've wasted a bunch of time on this and would appreciate any help. Thanks!
Daniel _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden