简体   繁体   English

Xcode:无效的参数不满意

[英]Xcode: Invalid parameter not satisfying

I keep running into a pretty frustrating error in Xcode after implementing a date picker. 在实现日期选择器之后,我在Xcode中遇到了一个非常令人沮丧的错误。 The error in the debugger is: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date" 调试器中的错误是:“因未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无效的参数不满足:date”

I've been going through my code for hours now, and can't find the issue. 我几个小时以来一直在查看我的代码,但找不到问题。 It may be because I'm not checking for nil, there is no date the first time the app installs and launches, so that may be causing the crash. 这可能是因为我没有检查nil,第一次安装和启动应用程序时没有日期,因此可能导致崩溃。 If it is, how do I check for nil in this code? 如果是,我如何在此代码中检查nil? I'm still very new at programming, any help would be much appreciated. 我在编程方面还很新,任何帮助都会非常感激。 Here is the code: 这是代码:

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

You don't check if date is null, before using it, in ex. 在使用之前,您不会检查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];
}

I've find a Debug way may help you debug where is the exception occurred and may help someone to debug with exception. 我发现调试方式可以帮助您调试发生异常的位置,并可以帮助某人调试异常。

  1. navigate to breakpoint 导航到断点 导航到断点

  2. Click the add button, and choose exception Breakpoint 单击“添加”按钮,然后选择“异常断点” 单击“添加”按钮,然后选择“异常断点”

  3. Add breakpoint and make exception type to Objective-C 添加断点并将异常类型设置为Objective-C 在此输入图像描述

  4. Run the code It will stop at the line which make crash happened! 运行代码它将停在发生崩溃的行!

Hope this help~ 希望这有帮助〜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:无效参数不令人满意:约束 - Swift: Invalid parameter not satisfying: constraint AFNetworking - 无效参数不满足:url - AFNetworking - Invalid parameter not satisfying: url 无效的参数不令人满意:nibNameMap!= nil - Invalid parameter not satisfying: nibNameMap != nil 无效的参数不令人满意:messageSenderId!= nil - Invalid parameter not satisfying: messageSenderId != nil RestKit映射无效参数不满足:managedObjectStore - RestKit Mapping Invalid parameter not satisfying: managedObjectStore UICollectionViewDiffableDataSource 崩溃:无效参数不满足:itemCount - UICollectionViewDiffableDataSource crash: Invalid parameter not satisfying: itemCount iOS RestKit问题:无效的参数不令人满意:responseDescriptors - iOS RestKit issue: Invalid parameter not satisfying: responseDescriptors NSInternalInconsistencyException: '无效参数不满足:!stayUp || CLClientIsBackgroundable(internal->fClient)' - NSInternalInconsistencyException: 'Invalid parameter not satisfying: !stayUp || CLClientIsBackgroundable(internal->fClient)' NSInternalInconsistencyException无效参数不满足:推送视图时的颜色 - NSInternalInconsistencyException Invalid parameter not satisfying: color when pushing view iOS13 DiffableDataSource 无效参数不满足:indexPath || 忽略无效项目 - iOS13 DiffableDataSource Invalid parameter not satisfying: indexPath || ignoreInvalidItems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM