Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: Invalid parameter not satisfying

I keep running into a pretty frustrating error in Xcode after implementing a date picker. The error in the debugger is: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: 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. If it is, how do I check for nil in this code? 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"];
}
like image 254
Henry F Avatar asked Jan 20 '12 06:01

Henry F


2 Answers

You don't check if date is null, before using it, in ex.

(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];
}
like image 106
Tomasz Wojtkowiak Avatar answered Nov 14 '22 02:11

Tomasz Wojtkowiak


I've find a Debug way may help you debug where is the exception occurred and may help someone to debug with exception.

  1. navigate to breakpoint navigate to breakpoint

  2. Click the add button, and choose exception Breakpoint Click the add button, and choose exception Breakpoint

  3. Add breakpoint and make exception type to Objective-C enter image description here

  4. Run the code It will stop at the line which make crash happened!

Hope this help~

like image 19
Jerome Li Avatar answered Nov 14 '22 02:11

Jerome Li