【问题标题】:Xcode: Invalid parameter not satisfyingXcode:无效参数不满足
【发布时间】:2012-02-14 18:58:02
【问题描述】:

在实现日期选择器后,我在 Xcode 中不断遇到一个非常令人沮丧的错误。调试器中的错误是:“由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效参数不满足:日期”

我已经检查了我的代码几个小时,但找不到问题。可能是因为我没有检查 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"];
}

【问题讨论】:

    标签: ios xcode parameters


    【解决方案1】:

    在使用日期之前,您不会检查日期是否为空,例如

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

    【讨论】:

    • 非常感谢您的帮助!你帮了我很大的忙!
    【解决方案2】:

    我找到了一种 Debug 方式可以帮助您调试异常发生在哪里,并且可以帮助某人调试异常。

    1. 导航到断点

    2. 点击添加按钮,选择异常断点

    3. 在 Objective-C 中添加断点和异常类型

    4. 运行代码 它会停在导致崩溃发生的那一行!

    希望对你有所帮助~

    【讨论】:

    • 我希望我早就知道这一点! +1!!
    • 设置异常断点非常有用,因为没有它非常困难。谢谢?
    猜你喜欢
    • 2021-07-20
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多