xcode DatePicker 和 NSDate 崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15219289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crash with DatePicker and NSDate
提问by Evgeniy Kleban
I have mysterious crash when trying to launch my app on iPhone (it perfectly valid works on Simulator though). I think problem is in this 2 methods..:
尝试在 iPhone 上启动我的应用程序时发生了神秘的崩溃(尽管它在模拟器上完全有效)。我认为问题在于这两种方法..:
-(void)viewWillAppear:(BOOL)animated{
[self getValueFromPicker];
}
-(void)getValueFromPicker{
NSDate *now = [NSDate date];
NSDate *birthDate = self.birthdayPicker.date;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:now
toDate:birthDate
options:0];
NSLog(@"Difference in years %i/", components.year);
}
App crash with SIGABRTerror, and there is a text i can see in a console: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
应用程序因SIGABRT错误而崩溃,我可以在控制台中看到一条文本:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
Please help me, i'am really have no idea what i did wrong, when i did the same thing on Simulator in console i can see difference in years from Now date and date, user picked.
请帮助我,我真的不知道我做错了什么,当我在控制台中的模拟器上做同样的事情时,我可以看到用户选择的现在日期和日期之间的差异。
Update:after i removed [self defaultBirthdayPickerDate]; in viewDidLoad section it start to work. But, now picker show current date, its not very convenient for picking birthday Date, now i still need to change its current date from now to past..
更新:在我删除 [self defaultBirthdayPickerDate] 之后;在 viewDidLoad 部分它开始工作。但是,现在选择器显示当前日期,它不是很方便选择生日日期,现在我仍然需要将其当前日期从现在更改为过去..
回答by iPatel
Mostly this types of error generated when you applies nil
date: check values of self.birthdayPicker.date
it is nil
or/not ?? and give proper condition if
you get nil
value of UIDatePicker.
大多数情况下,当您应用nil
日期时会产生这种类型的错误:检查self.birthdayPicker.date
它的值是否为nil
?? 并给出适当的条件,if
你得到的nil
价值UIDatePicker.
回答by Midhun MP
I think the issue is here:
我认为问题出在这里:
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:now
toDate:birthDate
options:0];
Your fromDate
is higher than toDate
.
你fromDate
的高于toDate
.
Change it to:
将其更改为:
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:birthDate
toDate:now
options:0];
回答by AppleDelegate
You need to set the locale of your NSDateFormatter.
Set up your iPhone settings with US locale and then set the 12/24 setting to any one option.
For that you have to look into the iphone settings panel.
您需要设置 NSDateFormatter 的语言环境。使用美国语言环境设置您的 iPhone 设置,然后将 12/24 设置设置为任一选项。
为此,您必须查看 iphone 设置面板。
Hope this works.
希望这有效。
回答by Evgeniy Kleban
I also have a method for setting default values for picker like this:
我还有一种为选择器设置默认值的方法,如下所示:
-(void)defaultBirthdayPickerDate{
NSString *dateString = @"09-Oct-1987";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"dd-MMM-yyyy";
NSDate *date = [dateFormatter dateFromString:dateString];
[self.birthdayPicker setDate:date];
}
-(void)viewDidLoad{
[self defaultBirthdayPickerDate];
}
Full crash report in console is:
控制台中的完整崩溃报告是:
2013-03-05 13:15:12.982 DeathLine[2506:907] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit/UIKit-2380.17/_UIDatePickerView.m:302
2013-03-05 13:15:12.985 DeathLine[2506:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
*** First throw call stack:
(0x325042a3 0x3a22197f 0x3250415d 0x32dd9ab7 0x344bdf15 0x344c6429 0x3458f989 0x35a6b 0x358db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x2908c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0x2dc21 0x3a658b20)
libc++abi.dylib: terminate called throwing an exception