[英]Xcode: Invalid parameter not satisfying
I keep running into a pretty frustrating error in Xcode after implementing a date picker. 在实现日期选择器之后,我在Xcode中遇到了一个非常令人沮丧的错误。 The error in the debugger is: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date" 调试器中的错误是:“因未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无效的参数不满足:date”
I've been going through my code for hours now, and can't find the issue. 我几个小时以来一直在查看我的代码,但找不到问题。 It may be because I'm not checking for nil, there is no date the first time the app installs and launches, so that may be causing the crash. 这可能是因为我没有检查nil,第一次安装和启动应用程序时没有日期,因此可能导致崩溃。 If it is, how do I check for nil in this code? 如果是,我如何在此代码中检查nil? I'm still very new at programming, any help would be much appreciated. 我在编程方面还很新,任何帮助都会非常感激。 Here is the code: 这是代码:
#import "DatePickerViewController.h"
@implementation DatePickerViewController
@synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Tomorrow!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
return YES;
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:@"DatePickerViewController.selectedDate"];
[self.datePicker setDate:storedDate animated:NO];
}
- (IBAction)dateChanged:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];
}
You don't check if date is null, before using it, in ex. 在使用之前,您不会检查date是否为null。
(void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:@"DatePickerViewController.selectedDate"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
// ---
[self.datePicker setDate:storedDate animated:NO];
}
I've find a Debug way may help you debug where is the exception occurred and may help someone to debug with exception. 我发现调试方式可以帮助您调试发生异常的位置,并可以帮助某人调试异常。
Click the add button, and choose exception Breakpoint 单击“添加”按钮,然后选择“异常断点”
Add breakpoint and make exception type to Objective-C 添加断点并将异常类型设置为Objective-C
Run the code It will stop at the line which make crash happened! 运行代码它将停在发生崩溃的行!
Hope this help~ 希望这有帮助〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.