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 <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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com