在Xcode上的IOS模拟器上,应用程序运行正常,没有崩溃,但是当我把它放到我的iPhone上并单击这个按钮时,即使所有其他按钮确实使应用程序崩溃,应用程序也会崩溃。
我没有崩溃日志报告,因为我不是注册开发人员,我有一个越狱设备。按钮中的代码基本上检查文本字段中是否有任何非法字符,或者“小时”文本字段是否超过24,获取小时字段并将其转换为秒,并查看是否已经启动计时器如果是,那么恢复,如果不是刚开始。
Calc_Start
是实际的按钮,update是在启动计时器时调用的方法。接口生成器中没有任何关联,因为它在模拟器中工作正常。如果有人能够看到我的代码中是否存在导致应用程序崩溃的任何内容,我将非常感激。
如果有可能,任何人都可以告诉我如何获取iPhone的“假”或第三方控制台应用程序,以便我可以看到崩溃报告,因为我不想获得开发者帐户,因为我从来不想放我的应用程序进入应用程序商店,这对我来说只是一个爱好。
这是我的代码:
- (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)
答案 0 :(得分:2)
崩溃的原因是 containsString 方法。
此方法是在iOS 8中引入的,您可能在iOS 8模拟器中运行应用程序,而您的设备在iOS 7上运行。
因此,对于iOS 7,您需要使用
if ([self.tf1.text rangeOfString:@"-"].location != NSNotFound) {
}
希望这会有所帮助......
答案 1 :(得分:0)
这可能对您有所帮助,只需替换您的代码:
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:@""];
}
你完成了! 一切顺利...... !!!