簡體   English   中英

Xcode:無效的參數不滿意

[英]Xcode: Invalid parameter not satisfying

在實現日期選擇器之后,我在Xcode中遇到了一個非常令人沮喪的錯誤。 調試器中的錯誤是:“因未捕獲的異常而終止應用程序'NSInternalInconsistencyException',原因:'無效的參數不滿足:date”

我幾個小時以來一直在查看我的代碼,但找不到問題。 這可能是因為我沒有檢查nil,第一次安裝和啟動應用程序時沒有日期,因此可能導致崩潰。 如果是,我如何在此代碼中檢查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"];
}

在使用之前,您不會檢查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];
}

我發現調試方式可以幫助您調試發生異常的位置,並可以幫助某人調試異常。

  1. 導航到斷點 導航到斷點

  2. 單擊“添加”按鈕,然后選擇“異常斷點” 單擊“添加”按鈕,然后選擇“異常斷點”

  3. 添加斷點並將異常類型設置為Objective-C 在此輸入圖像描述

  4. 運行代碼它將停在發生崩潰的行!

希望這有幫助〜

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM