Re: String variables in classes
Re: String variables in classes
- Subject: Re: String variables in classes
- From: "Stephen J. Butler" <email@hidden>
- Date: Sun, 2 Jan 2011 00:15:46 -0600
On Sat, Jan 1, 2011 at 3:00 PM, Brian Durocher <email@hidden> wrote:
> I have a quick question about member variables in a class. Is there a
> way to use an NSMutableString in a class as a member? I would like to
> make a string which accumulates equation text, in a calculator class. I
> have tried a number of methods to get this working with no success. The
> output of the NSMutableString is always Nil as is the NSString, I know
> there is an issue with initialization of the strings but I can not
> figure out how or why I cannot initialize them in this fashion. When
> using the member function and declaring the variable locally in that
> function there is no problem. Now I could pass out the Strings and
> handle the concatenation in my main() but I want to keep the data in
> the class and not in main. Can anyone shed some insight into why this is
> not working? Comments included in the code show where I have attempted
> initialization and when possible the exceptions created by the code.
>
> This is what I currently have
There is so much wrong here it's hard to know where to start. You're
kind of flailing around here without stopping to think about what the
Cocoa frameworks and design patterns say to do. Let me say four things
though:
- Mutable strings can certainly be instance variables of classes.
- You *never* call dealloc directly. You call release or autorelease
(one for each alloc, new, copy, copyMutable, or retain) and when the
release count goes to 0 the implementation in NSObject calls your
dealloc.
- You only call any init method on an instance once, right after an
alloc. If you call init on an instance you can't later call init,
initWithString:, etc. One init, once.
- You need to assign the return value from [super init] to self.
That's because [super init] does not have to return the same instance
that it was called on. Also, you need to check that self is not nil in
case the super's init failed.
> <code>
>
> #import <Foundation/Foundation.h>
>
> @interface Calculator : NSObject
> {
> double accumulator;
> NSMutableString *equation;
> //NSMutableString equation; //= [[NSMutableString alloc]
> initWithString:@"Starting Value - "];
> NSString *equationI;
>
> }
>
> - (id)init;
> -(void) dealloc;
> - (void) initialize;
> - (void) clear;
> - (void) total;
> - (void) destroy;
> - (double) accumulator;
> - (void) add: (double) n;
> - (void) subtract: (double) n;
> - (void) multiply: (double) n;
> - (void) divide: (double) n;
> - (void) exponent: (double) n;
>
>
>
> @end
>
>
> @implementation Calculator
> - (id) init
> {
> [super init];
> //equation = [[NSMutableString alloc] init]; //does not work
> //NSMutableString *equation = [[NSMutableString alloc] init];
> //causes exception, compiler says variable is not used.
> return self;
> }
>
> - (void)dealloc
> {
> //[myNewString release];
> [equation release];
> [equationI release];
> //[super dealloc]; // pointer being freed was not allocated
> }
>
> - (void) initialize
> {
> accumulator = 0;
>
> /*Some things i have tried */
> //equation = "Init";
> //NSMutableString equation = [[NSMutableString alloc] init];
> //NSMutableString *equation = [*equation appendString:"Init"];
> //NSMutableString *equationI = "Init2";
> //NSMutableString equation = [[NSMutableString alloc]
> initWithString:@"Init"];
> //equation = [[NSMutableString alloc] initWithString:@"Test 002"];
> //causes a crash
>
> //equation = [[NSMutableString alloc] initWithString:@"s"]; //causes
> exception
> //equationI = [[NSString alloc] init];
> equation = [equation initWithString:@"test"];
> equationI = [equationI initWithString:@"test2"];
> //[equation setString:@"test1"];
> //[equationI setString:@"test2"];
>
> NSLog(@"%@ \n", equation);
> NSLog(@"%@ \n", equationI);
>
> [equation appendString:@"Initialized"];
> //equation = @"Equation - ";
> //[equation setString: @"Equation:"];
> //[equationI appendFormat: @"%f", accumulator ];
>
> [equation stringByAppendingFormat: @"Equation - %f", accumulator ];
>
> }
> - (void) clear
> {
>
> }
> - (void) total
> {
> NSLog(@"Displaying NSMutableString %@ = %f", equation, accumulator);
> NSLog(@"Displaying NSString %@ = %f", equationI, accumulator);
>
> }
> - (void) destroy
> {
>
> }
> - (double) accumulator
> {
> return accumulator;
> }
> - (void) add: (double) n
> {
> accumulator += n;
> [equation appendFormat: @" + %f ", n];
>
> NSString* myNewString = [NSString stringWithFormat:@"%f", n];
> NSLog(@"%@", myNewString);
>
> [equation stringByAppendingFormat: @" + %f ", n];
> }
> - (void) subtract: (double) n
> {
> accumulator -= n;
> [equation appendFormat: @" - %f ", n];
> NSString* myNewString = [NSString stringWithFormat:@"%f", n];
> NSLog(@"%@", myNewString);
>
>
> [equation stringByAppendingFormat: @" - %f ", n];
>
> }
> - (void) multiply: (double) n
> {
> accumulator *= n;
> [equation appendFormat: @" * %f ", n];
> NSString* myNewString = [NSString stringWithFormat:@"%f", n];
> NSLog(@"%@", myNewString);
>
> [equation stringByAppendingFormat: @" * %f ", n];
>
> }
> - (void) divide: (double) n
> {
> accumulator /= n;
> [equation appendFormat: @" %f / %f ", n];
> NSString* myNewString = [NSString stringWithFormat:@"%f", n];
> NSLog(@"%@", myNewString);
>
> [equation stringByAppendingFormat: @" / %f ", n];
>
> }
> - (void) exponent: (double) n
> {
> accumulator = pow(accumulator, n);
> [equation appendFormat: @" ^ %f ", n];
> NSString* myNewString = [NSString stringWithFormat:@"%f", n];
> NSLog(@"%@", myNewString);
>
> [equation stringByAppendingFormat: @" ^ %f ", n];
>
> }
>
>
> @end
>
>
> int main (int argc, const char * argv[]) {
> NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
> // insert code here...
> Calculator *deskCalc = [[Calculator alloc] init];
>
> [deskCalc init];
> [deskCalc initialize];
> [deskCalc add:10];
> [deskCalc subtract:5];
>
> [deskCalc total];
> [deskCalc dealloc];
> [deskCalc release];
> NSLog(@"Hello, World!");
> [pool drain];
> return 0;
> }
>
> </code>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden