2012-01-20 13 views
16

Po uruchomieniu selektora daty napotykam na bardzo frustrujący błąd w Xcode. Błąd w debugerze jest następujący: "Kończenie aplikacji z powodu nieprzechwyconego wyjątku" NSInternalInconsistencyException ", powód:" Niepoprawny parametr nie spełnia: data "Xcode: Niepoprawny parametr nie spełniający oczekiwań

Przechodzę przez mój kod od wielu godzin i nie mogę znaleźć kwestia. Być może dlatego, że nie sprawdzam wartości zerowej, nie ma daty pierwszej instalacji i uruchomienia aplikacji, co może być przyczyną awarii. Jeśli tak, jak mogę sprawdzić zero w tym kodzie? Nadal jestem bardzo nowy w programowaniu, każda pomoc będzie bardzo ceniona. Oto kod:

#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"]; 
} 

Odpowiedz

22

Nie sprawdzasz, czy data jest zerowa, przed użyciem, np.

(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]; 
} 
+1

Dziękuję bardzo za pomoc! Bardzo mi pomogłeś! – John

Powiązane problemy