Re: That Cocoa Book...
Re: That Cocoa Book...
- Subject: Re: That Cocoa Book...
- From: John Hörnkvist <email@hidden>
- Date: Tue, 1 Jan 2002 00:47:56 +0100
On Monday, December 31, 2001, at 07:23 , Michael F Brinkman wrote:
I just bought the "Cocoa Programming for Mac OS X", and went to the web
page to
look for a solution to the challenge problem on page 102. There isn't a
solution to the challenge question posted.
I made an application called "LetterCounter", which works, but I wanted
to
compare my solution to the book's. I'm not really sure what I was
supposed to
do with allocating and deallocating memory, or if I'm even supposed to
do
something.
I don't have the book, so I can't tell what your goal is, but I'd either
move myString into the method where it is used, or keep a copy of it.
This is what I put in my Controller.h file:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet id inputField;
IBOutlet id outputField;
NSString *myString;
int myCount;
}
- (IBAction)countButton:(id)sender;
@end
This is what I put in my Controller.m file:
#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
[outputField setStringValue: @"???"];
}
- (IBAction)countButton:(id)sender
{
myString = [[inputField stringValue] copy]; // Make sure you own
the string.
myCount = [myString length];
[outputField setStringValue: [NSString stringWithFormat: @"%@
contains %d
letters", myString, myCount]];
}
- (void)dealloc
{
[myString release];
[super dealloc];
}
@end
Or:
- (IBAction)countButton:(id)sender
{
NSString* myString = [inputField stringValue];
myCount = [myString length];
[outputField setStringValue: [NSString stringWithFormat: @"%@
contains %d
letters", myString, myCount]];
}
Regards,
John Hornkvist
--
ToastedMarshmallow, the perfect Cocoa companion
http://www.toastedmarshmallow.com