Re: String variables in classes
Re: String variables in classes
- Subject: Re: String variables in classes
- From: Graham Cox <email@hidden>
- Date: Sun, 2 Jan 2011 17:25:30 +1100
On 02/01/2011, at 8:00 AM, Brian Durocher 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?
Yes, it requires no special considerations.
> 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;
>
> }
Well, you can't include executable code in the @interface part of your declaration, so the commented out lines could never work.
> @implementation Calculator
> - (id) init
> {
> [super init];
You should always use the standard idiom self = [super init]; if (self ){ .... }
> //equation = [[NSMutableString alloc] init]; //does not work
What does 'does not work' mean? It returns nil? It should be OK. You could also try equation = [[NSMutableString string] retain];
> //NSMutableString *equation = [[NSMutableString alloc] init];
> //causes exception, compiler says variable is not used.
That's not an exception, it's just a compiler or analyser warning.
>
> - (void)dealloc
> {
> //[myNewString release];
> [equation release];
> [equationI release];
> //[super dealloc]; // pointer being freed was not allocated
> }
Highly suspicious. You MUST call [super dealloc].
> - (void) initialize
This is nonsense. There's no instance method called -initialize. There is the class method +initialize, did you mean that? If you didn't, and this is literally what you have, it would never be called as the runtime doesn't know anything about this method. In any case, you shouldn't be using +initialize to init instances of your calculator object - that's what its -init method is for.
I'll ignore the rest of this as it isn't likely to bear fruit.
> [equation stringByAppendingFormat: @" + %f ", n];
This returns another string - you are ignoring it. This won't append things to the existing mutable string 'equation'.
> [equation stringByAppendingFormat: @" - %f ", n];
Ditto.
> [equation stringByAppendingFormat: @" * %f ", n];
Ditto.
> [equation stringByAppendingFormat: @" / %f ", n];
Ditto.
> [equation stringByAppendingFormat: @" ^ %f ", n];
Ditto.
> [deskCalc dealloc];
AROOOOGAHHHH!!!!!! Oops, the alarm just went off. NEVER DO THIS. Just call -release.
> [deskCalc release];
Now we just crashed.
Usual stuff applies - read the memory management guidelines, etc, etc ad nauseam. You need to know it well before coding a single line.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
--Graham
_______________________________________________
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