我只是“isEqualToString:”来比较我的 iPhone 应用程序中的字符串。var 值是文本,“subInput”是文本字段中的字符串。到目前为止一切顺利,当输入字段的第一个字符与文本的第一个字符相同时。但是当它们不适合时,应用程序就会崩溃。在开始良好后输入错误的字符时,一切正常。
subInput = [[NSString alloc] initWithString:[theInput text]];
compare = [[NSString alloc] initWithString:[value substringToIndex:subInput.length]];
if([subInput isEqualToString:compare]){
//Here the app stops working
我还尝试将结果与“!= 0”或“!= nil”进行比较,但也没有用。
附加代码
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];
}