【问题标题】:objective-c/cocoa-touch - comparison between two NSStrings failing even though they are identicalobjective-c/cocoa-touch - 两个 NSStrings 之间的比较失败,即使它们是相同的
【发布时间】:2010-09-30 00:29:57
【问题描述】:

我有一个比较两个 NSString 的 if 语句,一个是来自 UITextField 的用户输入,另一个是从 0-9 之间的随机整数创建的 NSString,但是每次比较都失败,即使它们是相同的,如图所示来自 NSLog 调用的日志。那么任何人都可以在提供的代码中看到我做错了什么吗?

-(void) generateDecryptionCode{
    codeToConfirm = [NSString stringWithFormat:@"%i%i%i%i%i%i", arc4random()%10, arc4random()%10, arc4random()%10, arc4random()%10, arc4random()%10, arc4random()%10];
    numberToDecrypt.text = codeToConfirm;
}
- (void) decryptTimerFires{
    NSURL *beep = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Beep.aif", [[NSBundle mainBundle] resourcePath]]];
    NSURL *buzz = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Buzz.aif", [[NSBundle mainBundle] resourcePath]]];
        if(decryptTime > 0){
            decryptTime--;
            decryptLabel.text = [NSString stringWithFormat:@"%g",  (float)decryptTime/10];
        if(decryptTime%10 == 0){
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:beep error:nil];
            audioPlayer.numberOfLoops = 1;
            [audioPlayer play];
        }
    }
    else{
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:buzz error:nil];
        audioPlayer.numberOfLoops = 1;
        [audioPlayer play];
        [decryptTimer invalidate];
        decryptTimer = nil;
    }

}
-(void) stopDecrypt{
    NSLog(@"Stop Decrypt");
    [decryptTimer invalidate];
    decryptTimer = nil;

}
-(IBAction)decrypt{
    [self generateDecryptionCode];
    decryptTime = 200;
    decryptTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(decryptTimerFires) userInfo:nil repeats:YES];
    [decryptTimer fire];

}
- (void)dealloc {
    [decryptLabel release];
    [decryptButton release];
    [crackLabel release];
    [crackButton release];
    [numberToCrack release];
    [numberToDecrypt release];
    [audioPlayer release];
    [super dealloc];
}

-(void) enterDecryptKey{
    confirm = [[UIAlertView alloc] initWithTitle:@"Confirm Code" message:@"Please Input The Correct Code:"  delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm Code", nil];
    inputCode = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);

    [confirm setTransform:myTransform];
    [inputCode setBackgroundColor:[UIColor whiteColor]];
    [confirm addSubview:inputCode];
    [confirm show];
    [confirm release];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   NSLog(@"Enter code:");
    if(buttonIndex == 0){
        [confirm dismissWithClickedButtonIndex:0 animated:YES];
    }
    else if(buttonIndex == 1){
        NSLog(@"Code:%@/User Input:%@",codeToConfirm, inputCode.text);
        NSLog(@"comparing code...");
        if (inputCode.text == codeToConfirm) {
            NSLog(@"Code Correct");
            [self stopCrack];
            [self stopDecrypt];
        }
    }
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [inputCode resignFirstResponder];
    [inputCode removeFromSuperview];  
    [inputCode release];  
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    您应该将字符串与- (BOOL) isEqualToString- (BOOL) isEqual 进行比较,如果两个指针指向相同的地址,== 才会返回 true

    【讨论】:

    • 非常感谢,这一直困扰着我一下午,我没有意识到 == 是这样工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多