2012-06-28 2 views
0

"isEqualToString :"그냥 내 iPhone 응용 프로그램의 문자열과 비교하면됩니다. var 값은 텍스트이고 "subInput"은 텍스트 필드 밖의 문자열입니다. 지금까지 입력 필드의 첫 번째 문자가 텍스트의 첫 번째 문자와 같을 때 모든 것이 잘되었습니다. 그러나 그들이 적합하지 않으면 App이 충돌합니다. 잘 시작한 후에 잘못된 문자를 입력하면 모든 것이 정상입니다. NSString 형식의 두 문자열을 비교할 때 응용 프로그램이 충돌 함

subInput = [[NSString alloc] initWithString:[theInput text]]; 
compare = [[NSString alloc] initWithString:[value substringToIndex:subInput.length]]; 

if([subInput isEqualToString:compare]){ 
//Here the app stops working 

가 나는 또한 함께 결과를 비교하기 위해 노력했다 "! = 0"또는 "! = 전무"또한 작동하지 않았다.


if([subInput isEqualToString:compare] != nil){ 
     NSLog(@"any"); 
     if([subInput isEqualToString:value]){ 
      NSLog(@"other"); 
      NSLog(@"well done"); 
      theInput.enabled = FALSE; 
      lastValid = theInput.text; 
      theMessage.backgroundColor = [UIColor orangeColor]; 
      theMessage.text = @"WELL DONE!!!!"; 
      theMessage.textColor = [UIColor blackColor]; 

      //save points for remaining seconds 
      score += seconds*10; 
      //invalidate counter when typed in all text correct 
      [count invalidate]; 
      [self newTimer:3.0]; 
     }else{ 

      if(theInput.text.length >= range){ 
       NSLog(@"SCROLLEN"); 
       [theText setSelectedRange:NSMakeRange(range, addRange)]; 
       range += addRange; 
      } 
      //NSLog(@"String is ok"); 
      //self.view.backgroundColor = [UIColor blackColor]; 
      lastValid = theInput.text; 
      theMessage.backgroundColor = [UIColor greenColor]; 
      theMessage.text = @"GO GO GO"; 
      theMessage.textColor = [UIColor whiteColor]; 
     } 

    } else{ 
     //incrementing the fail var for every wrong letter 
     fails++; 

     theInput.text = [@"" stringByAppendingString:lastValid]; 
     theMessage.backgroundColor = [UIColor redColor]; 
     theMessage.text = @"WRONG CHARACTER!!!"; 
     theMessage.textColor = [UIColor whiteColor]; 
     if (grade >= 2) { 
      seconds -= 2; 
     } 

     if(grade == 3){ 
      int actual = theInput.text.length; 
      actual--; 
      NSString *shorterString = [theInput.text substringToIndex:actual]; 
      lastValid = shorterString; 
      theInput.text = shorterString; 

     } 
     //change bg-color to iritate the player 
     self.view.backgroundColor = [UIColor colorWithRed:[self r:255]/255.0 green:[self r:255]/255.0 blue:[self r:255]/255.0 alpha:1]; 
    } 
+1

예외는 무엇입니까? – Wug

+0

더 많은 코드를 볼 수 있습니까? 특히, 당신이 예외를 얻는 라인과'if' 문에 어떤 코드. – Peter

+0

'[value substringToIndex : subInput.length]'가 범위를 벗어났습니다. 초기화와 두 문자열의 비교는 괜찮은 것 같습니다. – DrummerB

답변

0

당신은 확인하고 값의 길이가 subInput의 길이보다 큰 경우 볼 필요가 추가 CODE. 그렇지 않다면, 당신은 거기서 벗어날 것입니다.

0

또한 [theInput text]가 nil이 아닌지 확인해야합니다. [있는 NSString initWithString : (있는 NSString *) aString]에

subInput = [[NSString alloc] initWithString:[theInput text]]; 

문서는 당신이 전무 값으로 초기화하려고하면 당신은 예외를 얻을 것이 분명하다.

관련 문제