gpt4 book ai didi

iphone - 比较两个 NSString 类型的字符串时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 22:53:42 27 4
gpt4 key购买 nike

我只是将“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];
}

最佳答案

您还需要检查 [theInput text] 是否为 NOT nil。

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

[NSString initWithString:(NSString *)aString] 上的文档清楚地表明,如果您尝试使用 nil 值进行初始化,则会出现异常。

关于iphone - 比较两个 NSString 类型的字符串时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com