iPhoneアプリの文字列と比較するには、「isEqualToString:」だけです。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];
}