2010-09-30 18 views
1

我有一個if語句比較兩個NSString,其中一個是來自UITextField的用戶輸入,另一個是從0-9之間的隨機整數創建的NSString包括在內,但是每次都比較失敗,即使它們在NSLog調用的日誌中顯示的相同。所以任何人都可以看到我在提供的代碼中做錯了什麼?objective-c/cocoa-touch - 兩個NSString之間的比較失敗,即使它們是相同的

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

回答

3

你應該- (BOOL) isEqualToString字符串比較或- (BOOL) isEqual==將返回只有在兩個指針指向同一個地址

+0

非常感謝你這整個下午都纏着我的話,我不知道==那樣工作。 – Joe 2010-09-30 00:51:10

相關問題