开发者

Comparing two strings in objective-c causes app to quit

开发者 https://www.devze.com 2023-02-06 19:01 出处:网络
I am attempting to compare two strings using \"isEqualToString\" and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I\'m quite sure that the strings are in fac

I am attempting to compare two strings using "isEqualToString" and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I'm quite sure that the strings are in fact equal to each other and despite my best attempts I'm out of ideas. Any help would be appreciated.

-(void)createTextfields{
    name = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
    name.borderStyle = UITextBorderStyleRoundedRect;
    name.textColor = [UIColor blackColor];
    name.placeholder = @"Password";
    name.textAlignment = UITextAlignmentCenter;
    [self addSubview:name];

    entry = [[UITextField alloc] initWithFrame:CGRectMake(170, 0, 140, 25)];
    entry.borderStyle = UITextBorderStyleLine;
    entry.textColor = [UIColor blueColor];
    entry.textAlignment = UITextAlignmentCenter;
    [self addSubview:entry];
}

-(void)createSubmitButton{
    UIButton *submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    submit.titleLabel.textAlignment = UITextAlignmentCenter;
    [submit setTitle:@"Submit" forState:UIControlStateNormal];
    submit.frame = CGRectMake(90, 50, 60, 30);
    [submit addTarget:self action:@selector(runSubmit) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:submit];


    UIButton *savePass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    savePass.titleLabel.textAlignment = UITextAlignmentCenter;
    [savePass setTitle:@"Save" forState:UIControlStateNormal];
    savePass.frame = CGRectMake(20, 50, 60, 30);
    [savePass addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:savePass];
}

-(void)save{

    tempPass = name.text;
    name.text = @"";
    entry.text = tempPass;

}

-(void)runSubmit{

//  password = name.text;

    if ([tempPass isEqualToString:name.text]) {
        //[viewController displayAlertFromViewControl];
    }else {
        开发者_StackOverflow中文版UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct" message:@"Alert" delegate: self cancelButtonTitle:@"Close" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }


}


Probably one of your strings (pretty sure that it is tempPass) is autoreleased


Where is tempPass being declared? Is it possible that it isn't being retained long enough to be referenced in the if?


If you run your program with this menu item: Run -> Run with Performance Tool -> Zombies then Instruments will do all the hard work of tracking down the problem for you. It will show you which object was released early and where it was allocated.

It is probably tempPass. Once you determine if that is the case, it would be best to make tempPass a property with a copy attribute and then use self.tempPass=name.text instead of just tempPass=name.text.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号