我纔剛剛「isEqualToString」崩潰。 var值是一個文本,「subInput」是一個不在文本字段中的字符串。到目前爲止,當輸入字段的第一個字符與文本的第一個字符相同時,一切進展順利。但是,當他們不適合時,應用程序崩潰。開始後輸入錯誤的字符時,一切正常。應用比較型的兩個字符串時的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];
}
你有什麼異常? – Wug
我們可以看到更多的代碼嗎?特別是,你得到異常的行和'if'語句中的一些代碼。 – Peter
我猜'[value substringToIndex:subInput.length]'超出了界限。兩個字符串的初始化和比較似乎很好。 – DrummerB