site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type :content-transfer-encoding; bh=3CKqwVaVD/9vYSUSG9GE2hDzY4822CN/gAc7E8XargU=; b=xqc6JctPRL/cHWjorOGYILIajnqWa1rY44047BammhyAN2HfrHEUDs8cpxiAHPtCI7 Z1+81WTM0caRanZEeTQ564ScLLckoPhJ2Ghmt6VH5ybUcjzqHJyl5zuX+L09N/oo/Qq7 5HoxJPKE0Pv0VlvXqR4m+m8fxrn57UV7yI1w0= Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; b=d5372I8DLVPKu/4AGj1C5Sxm+glyK9vh2+TLn2Ep/cT/D/CcsznjpMfO/Qr+RJ+twx c0zDN05Cf1RUHS2CGHvexbcouOrRURnnPHriGM24t9FdStFOdMdcBlD10iftnuEGJv9t ZY/EuC7UJmZgg5C6xSCLQbbV8mEeITj8L1RzY= User-agent: Thunderbird 2.0.0.21 (Macintosh/20090302) 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/site_archiver%40lists.apple... This email sent to site_archiver@lists.apple.com