2012-01-20 4 views
16

日付ピッカーを実装した後、Xcodeでかなり不満なエラーに陥っています。デバッガのエラー:「キャッチされていない例外「NSInternalInconsistencyException」のためアプリケーションを終了します。理由:「無効なパラメータが満たされていません:日付」Xcode:無効なパラメータが不十分です

私は数時間前からコードを調べています問題。私は何もチェックしていないので、アプリがインストールされて起動する最初の日付がないので、クラッシュの原因となっている可能性があります。そうであれば、このコードでnilをどうやってチェックするのですか?私はプログラミングでまだ非常に新しいです、どんな助けも大いに感謝されるでしょう。コードは次のとおりです。

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

答えて

22

日付が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]; 
} 
+1

ありがとうございました!あなたは私を大いに助けました! – John

関連する問題