iPhone中字符串的重新初始化问题

时间:2011-05-30 07:25:33

标签: iphone objective-c

我正在创建一个程序,我必须初始化并重新初始化我的NSString变量。我编写了相关代码,但是当我尝试重新初始化我的NSString变量时,我的程序正在崩溃。我的macdelegate.h文件列在这里。

@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];

}

我所有列出的代码都在上面,当我第一次运行我的程序这是初始化时,但当我尝试重新初始化我的程序时,我没有做任何事情,我想这是崩溃。 我是Objective-C的新手帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为,您已忘记保留变量 waitingOperation 综合语句,并将其用作self.waitingOperation会导致崩溃,因为运行时系统无法找到属性 waitingOperation

的setter函数

实施文件中添加@synthesize waitingOperation;

如果您仍然遇到崩溃,请使用修改过的代码告诉我。

-(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";        
    }

}