Reinitialization problem of string in iPhone
I am making a program and i have to init开发者_如何学编程ialize and reinitialize my NSString variable.I wrote related code but when am trying to reinitialize my NSString variable my program is being crashed. My macdelegate.h file are listed here.
@interface MacCalculatorAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIButton *digitPressed;
UIButton *operatorPressed;
NSString *waitingOperation;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property ( retain) NSString* waitingOperation;
-(IBAction) digitPressed:(UIButton *)sender;
-(IBAction) operatorPressed:(UIButton *)sender;
@end
And part of code of my macdelegate.m file are listed here.
- (id) init
{
if (self = [super init])
{
self.waitingOperation=@"not set";//@"not set";
}
return self;
}
-(IBAction) digitPressed:(UIButton *)sender {
if(![@"not set" isEqualToString:waitingOperation])
{
waitingOperation=@"not set";//@"not set";
}
}
-(IBAction) operatorPressed:(UIButton *)sender {
NSString *operand=[[sender titleLabel] text];
resultDisplay.text=operand;
if([@"+" isEqualToString:operand])
{
waitingOperation=@"+";//@"not set";
}
}
- (void)dealloc {
[waitingOperation release];
[window release];
[super dealloc];
}
My all listed codes are above when i am running my program first time this is initialize well but when i am trying to reinitialize my program is doing nothing i suppose this is being crashed. I am new with Objective-C help me.
Thanks in advance.
I think, you have forgotten synthesize statement for your retaining variable waitingOperation and using it as self.waitingOperation
would cause a crash, because run time system could not find a setter function for the property waitingOperation
Add @synthesize waitingOperation;
in your implementation file.
Use the modified code let me know if you still get the crash.
-(IBAction) digitPressed:(UIButton *)sender
{
if(![ self.waitingOperation isEqualToString:@"not set"])
{
self.waitingOperation=@"not set";//@"not set";
}
}
-(IBAction) operatorPressed:(UIButton *)sender {
NSString *operand=[[sender titleLabel] text];
resultDisplay.text=operand;
if([operand isEqualToString:@"+"])
{
self.waitingOperation=@"+";//@"not set";
}
}
精彩评论