gpt4 book ai didi

ios - Xcode:无效参数不令人满意

转载 作者:IT王子 更新时间:2023-10-29 08:11:21 26 4
gpt4 key购买 nike

在实现日期选择器后,我一直在 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"];
}

最佳答案

在使用它之前,你不检查日期是否为空,例如。

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

关于ios - Xcode:无效参数不令人满意,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8937544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com