应用程序在单击按钮时崩溃,但在 iOS 模拟器上不会

application crashes at a click of a button but not on the iOS simulator

在 Xcode 上的 IOS 模拟器上,应用程序运行良好,没有崩溃,但是当我把它放在我的 iPhone 上并单击这个特定按钮时,即使应用程序崩溃了所有其他按钮确实会使应用程序崩溃。

我没有崩溃日志报告,因为我不是注册开发人员,我有一台越狱设备。按钮中的代码基本上检查文本字段中是否有任何非法字符,或者 "hours" 文本字段是否超过 24,获取小时字段并将其转换为秒,并检查计时器是否已经开始,如果是,则继续,如果不是,则开始。

Calc_Start 是实际的按钮,update 是定时器启动时调用的方法。界面生成器中没有任何链接错误,因为它在模拟器中运行良好。如果有人能看到我的代码中是否有任何导致应用程序崩溃的内容,我将不胜感激。

如果可能,谁能告诉我如何为 iPhone 获取 "fake" 或第三方控制台应用程序,以便我可以查看崩溃报告,因为我不想获得到期的开发者帐户事实上,我从来不想把我的应用程序放到应用程序商店,这对我来说只是一种爱好。

这是我的代码:

- (IBAction)Calc_Start:(id)sender
{
    float b = [self.tf1.text floatValue];
    float bb = [self.tf2.text floatValue];

    if (b > 24)
    {
        self.tf1.text = @"24";
    }
    if (bb > 24)
    {
        self.tf2.text = @"24";
    }

    if ([self.tf1.text isEqualToString:@""])
    {
        self.tf1.text = @"0";
    }
    if ([self.tf2.text isEqualToString:@""]) // if any of the text fields are empty, add 0's to them
    {
        self.tf2.text = @"0";
    }
    if ([self.tf3.text isEqualToString:@""])
    {
        self.tf3.text = @"0";
    }
    if ([self.tf4.text isEqualToString:@""])
    {
        self.tf4.text = @"100";
    }

    if ([self.tf1.text containsString:@"-"])
    {
        self.tf1.text = [self.tf1.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    if ([self.tf2.text containsString:@"-"])
    {
        self.tf2.text = [self.tf2.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    if ([self.tf3.text containsString:@"-"])
    {
        self.tf3.text = [self.tf3.text stringByReplacingOccurrencesOfString:@"-" withString:@""]; // if any of the text fields have a "-" in them then get rid of them
    }
    if ([self.tf4.text containsString:@"-"])
    {
        self.tf4.text = [self.tf4.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }


    float i = [self.tf1.text floatValue];
    float ii = [self.tf2.text floatValue];
    float iii = [self.tf3.text floatValue]; // gets the integer value of the strings inside the text fields
    float iv = [self.tf4.text floatValue];

    float secondCount = i * 3600;               // converts the first 2 to seconds to be used in the timer
    float ubsecondCount = ii * 3600;

    if ([self.countdown_label.text  isEqual: @"00:00:00"])
    {
        secondsLeft = secondCount - ubsecondCount; // used in the update method
    }
    else
    {
        secondsLeft = savedSeconds;
    }

    perSecondd = (iii * (iv/100))/3600;
    NSString *prSecond = [NSString stringWithFormat: @"$%.5f", perSecondd];

    float perMinutee = (iii * (iv/100))/60;
    NSString *prMinute = [NSString stringWithFormat:@"$%.3f", perMinutee];

    self.perSecond.text = prSecond;
    self.perMinute.text = prMinute;

    if (![self.tf1.text isEqualToString:@"0"])
    {
        if (![timer isValid])
        {
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                     target:self
                                                   selector:@selector(update)
                                                   userInfo:nil
                                                   repeats:true];

            [self.calc_button_atr setTitle: @"Calculate / Stop"
                                  forState: UIControlStateNormal];
        }
        else
        {
            [timer invalidate];
            [self.calc_button_atr setTitle: @"Calculate / Start"
                              forState: UIControlStateNormal];
        }
    }
}

- (void) update
{
    if(secondsLeft > 0 )
    {
        secondsLeft--;
        int hours = secondsLeft / 3600;
        int minutes = (secondsLeft % 3600) / 60;
        int seconds = (secondsLeft % 3600) % 60;
        self.countdown_label.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

        p = p + perSecondd;
        NSString *prSec = [NSString stringWithFormat: @"$%.5f", p];
        self.mps.text = prSec;

        savedSeconds = secondsLeft;
    }
    else
    {
        [timer invalidate];
        [self.calc_button_atr setTitle:@"Calculate / Start"
                              forState: UIControlStateNormal];
    }
}

这是崩溃报告:

015-01-06 15:04:44.703 Your Pay Per Second[10612:60b] -[__NSCFString containsString:]:      unrecognized selector sent to instance 0x1558ffc0
2015-01-06 15:04:44.712 Your Pay Per Second[10612:60b] *** Terminating app due to uncaught   exception 'NSInvalidArgumentException', reason: '-[__NSCFString containsString:]: unrecognized  selector sent to instance 0x1558ffc0'
*** First throw call stack:
(0x2d746f83 0x37ecaccf 0x2d74a917 0x2d749203 0x2d698768 0x86f8b 0x2ff99037 0x2ff98fd7 0x2ff98fb1     0x2ff84717 0x2ff98a2f 0x2ff98701 0x2ff936cb 0x2ff688cd 0x2ff66f77 0x2d71220b 0x2d7116db 0x2d70fecf  0x2d67aebf 0x2d67aca3 0x32580663 0x2ffc714d 0x89511 0x383d7ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这可能对您有帮助,只需替换您的代码:

if ([self.tf1.text containsString:@"-"])
{
    self.tf1.text = [self.tf1.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
}

使用此代码:

if ([self.tf1.text rangeOfString:@"-"].location != NSNotFound) {
        NSLog(@"String is found");
        NSLog(@"text %@",self.tf1.text);
        self.tf1.text = [self.tf1.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }

大功告成!!! 祝一切顺利...!!!

崩溃的原因是 containsString 方法。

此方法在 iOS 8 中引入,您可能 运行 您的应用程序在 iOS 8 模拟器中并且您的设备 运行 iOS7.

因此对于 iOS 7,您需要使用

if ([self.tf1.text rangeOfString:@"-"].location != NSNotFound) {
}

希望这对您有所帮助...