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

标签 iphone objective-c ios

我只是将“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/

相关文章:

ios - CFStreamCreateBoundPair 不能释放

iOS - 注销后重置 UITabBarController/UIViewController 状态

ios - 使用函数重新加载表/数组?

c++ - 指向 objc 类成员方法的函数指针,用于 C++?

ios - 将 PFObject 分配给 UILabel - 将 PFUser 转换为 NSString 值?

objective-c - 尝试制作 sqlite 的可写副本时出现错误

iphone - 如何在 mkMapview 上显示当前位置的详细信息?

iphone - YouTube api v3 对 iOS 视频的评论

iphone - 架构 armv7 的重复符号

ios - 应用程序在后台运行时如何获取室内位置X和Y位置?