2012-01-20 14 views
16

Continuo a incorrere in un errore piuttosto frustrante in Xcode dopo l'implementazione di un selezionatore di date. L'errore nel debugger è: "Terminazione dell'app a causa dell'eccezione non rilevata" NSInternalInconsistencyException, motivo: "Parametro non valido non soddisfacente: data"Xcode: parametro non valido non soddisfacente

Ho passato il mio codice per ore e non riesco a trovare il problema. Può essere perché non sto controllando nulla, non c'è data la prima volta che l'applicazione si installa e si avvia, quindi potrebbe essere la causa del crash. Se lo è, come faccio a verificare la presenza di nil in questo codice? Sono ancora molto nuovo alla programmazione, ogni aiuto sarebbe molto apprezzato. Ecco il codice:

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

risposta

22

Non si controlla se la data è nullo, prima di utilizzarla, nell'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]; 
} 
+1

Grazie mille per il vostro aiuto! Mi hai aiutato tremendamente! – John

Problemi correlati