Iphone 如何检查userdefaults中数组的每个对象
我正在将从数组中的选择器中选择的日期保存到userdefaults中。因此,当我选择一个特定日期并单击“保存”时,该特定日期将保存在userdefaults中。这是正确的。但现在我遇到了问题。我需要将userdefaults的每个日期和时间与当前时间进行比较。如果某个对象与当前日期和时间相等,则应显示通知。这是如何实现的可能,谢谢。这是我的密码 在这里,我通过日期选择器选择日期,并将日期保存在userdefaults中Iphone 如何检查userdefaults中数组的每个对象,iphone,objective-c,Iphone,Objective C,我正在将从数组中的选择器中选择的日期保存到userdefaults中。因此,当我选择一个特定日期并单击“保存”时,该特定日期将保存在userdefaults中。这是正确的。但现在我遇到了问题。我需要将userdefaults的每个日期和时间与当前时间进行比较。如果某个对象与当前日期和时间相等,则应显示通知。这是如何实现的可能,谢谢。这是我的密码 在这里,我通过日期选择器选择日期,并将日期保存在userdefaults中 this is my timepickercontroller.m - (
this is my timepickercontroller.m
- (void)viewDidLoad {
time = [[NSString alloc] init];
CGRect pickerFrame = CGRectMake(0,40,0,0);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePicker.datePickerMode = UIDatePickerModeTime;
datePicker.date = [NSDate date];
[datePicker addTarget:self action:@selector(convertDueDateFormat) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
-(void)convertDueDateFormat{
app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
app.timerdate = [self.datePicker date];
NSLog(@"picker date:%@",selecteddate);
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"time"]==nil)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"time"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
}
else
{
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"time"];
NSMutableArray *array_dates = [[NSMutableArray alloc] init];
for(int i =0;i<[array count];i++)
{
[array_dates addObject:[array objectAtIndex:i]];
}
[array_dates addObject:app.timerdate];
[[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:@"time"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
}
}
this is the controller where i am setting notification
-(IBAction)save{
dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSDate *currentime = [NSDate date];
currentcheck = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:currentime] ];
NSLog(@"currenttime:%@",currentcheck);
timepicker = [[TTimePickerController alloc]init];
if (app.timerdate == NULL && interval == NULL && newsound == NULL)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Set Date,interval,sound for alarm" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
else
{
[self scheduleNotification];
[self saveInDatabase];
}
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication]cancelAllLocalNotifications];
timepicker = [[TTimePickerController alloc]init];
NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
NSLog(@"fireDate=%@", newtestdate);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:newtestdate];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:newtestdate];
NSDateComponents *dateComps = [[NSDateComponents alloc]init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls!= nil) {
UILocalNotification *notif = [[cls alloc]init];
notif.fireDate = itemDate;
[app.dateFormatter setDateFormat:@"hh:mm a"];
newstring = [app.dateFormatter stringFromDate:notif.fireDate];
NSLog(@"new fire date:%@",newstring);
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Alarm";
notif.alertAction = @"View";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber=1;
notif.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
[notif release];
}
}
这是我的timepickercontroller.m
-(无效)viewDidLoad{
时间=[[NSString alloc]init];
CGRect pickerFrame=CGRectMake(0,40,0,0);
datePicker=[[UIDatePicker alloc]initWithFrame:pickerFrame];
datePicker.datePickerMode=UIDatePickerModeTime;
datePicker.date=[NSDate-date];
[datePicker addTarget:self action:@selector(convertDueDateFormat)for ControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[日期选择器发布];
}
-(void)convertDueDateFormat{
app=(StopSnoozeAppDelegate*)[[UIApplication sharedApplication]委托];
app.timerdate=[self.datePicker date];
NSLog(@“选取者日期:%@”,选择日期);
如果([[NSUserDefaults standardUserDefaults]valueForKey:@“time”]==nil)
{
[[NSUserDefaults standardUserDefaults]设置对象:[NSDate date]forKey:@“time”];
[[NSUserDefaults standardUserDefaults]同步];
NSLog(@“%@,[[NSUserDefaults standardUserDefaults]objectForKey:@“time”]);
}
其他的
{
NSArray*数组=[[NSUserDefaults standardUserDefaults]objectForKey:@“time”];
NSMutableArray*数组_dates=[[NSMutableArray alloc]init];
对于(int i=0;i
}我怀疑您真正想做的不是比较日期,而是将通知的触发日期设置为从阵列中获取的日期
为此,请枚举阵列并为每个timerDate启动NSNotification:
NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:@"time"];
NSDate *now = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
// .... general setup of all notifs here.
for (NSDate * aDate in timerDates) {
localNotif.fireDate = aDate;
//... further setup of each individual notif here....
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
[localNotif release];
但是,从您的代码来看,您似乎从默认值中获得了一个日期:
NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
但是您在方法中保存了一个数组
-(void)convertDueDateFormat{
...
[[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:@"time"];
那是哪一个呢?
如果有数组,则可以将其值作为日期进行测试:
NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:@"time"];
NSDate *now = [NSDate date];
_block int timerIndex; // can be changed inside the block
// use a block to enumerate the array
[timerDates enumerateObjectsUsingBlock:^(id obj,NSUInteger index, BOOL *stop){
if(fabs([(NSDate *)obj timeIntervalSinceDate:now])< 1.0) {
*stop=YES;
timerIndex = index;
}
} ];
当然,也可以查看用于比较日期的文档(NSDate的Apple类文档很好)。这只是一种方法,可能不是最好的方法。
(对我的格式设置很抱歉。我对这个有点陌生)为什么要直接检查设置本地通知的时间?使用objectForKey方法和NSUserDefaults检索数组。然后可以使用简单的for循环将当前时间与数组中的每个对象进行比较。希望如此helps@booleanBoy请你详细解释一下或者提供一些代码,因为我从来没有这样做过习惯于nsuserdefaults@booleanBoy如何将当前时间与arraybut suz中的每个对象进行比较我有点困惑。例如,如何设置与arrayStep匹配的日期步骤1:检查文档中的NSNotification!!firedate不是NSString,而是NSDate。步骤2:检查文档中的NSDate。NSDate是自格林威治标准时间2001年1月1日00:00:00以来的秒数。这是一个数字,与时区无关。NSDate将在NSDate发送通知。当您将NSDate保存为默认值时,它与用户默认时区在日期选择器中显示的日期相关。如果不是,请将firedate设置为中的值阵列。
NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:@"time"];
NSDate *now = [NSDate date];
_block int timerIndex; // can be changed inside the block
// use a block to enumerate the array
[timerDates enumerateObjectsUsingBlock:^(id obj,NSUInteger index, BOOL *stop){
if(fabs([(NSDate *)obj timeIntervalSinceDate:now])< 1.0) {
*stop=YES;
timerIndex = index;
}
} ];
localNotif.firedate = [timerDates objectAtIndex:timerIndex];