我有一个类似的数组:
_combinedBirthdates(
"03/05/2013",
"09/22/1986",
"03/02/1990",
"03/02",
"08/22/1989",
"11/02/1990",
"07/08",
"08/31/1990",
"05/13",
"07/11/2007",
"10/07/2010",
"02/20/1987")
如果今天的日期与上面数组中的日期相同,我想要本地通知
我使用以下逻辑进行通知:
NSLog(@" _combinedBirthdates%@",_combinedBirthdates);
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];
for (int i=0;i<_combinedBirthdates.count;i++)
{
NSDate *date =[Formatter1 dateFromString:[_combinedBirthdates objectAtIndex:i ]];
if(date == todaydate){
localNotif.fireDate = date;
localNotif.alertBody = @"birthdate notification";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
现在我的问题:
答案 0 :(得分:2)
查看修改后的代码....
NSMutableArray *newBirthDates = [[NSMutableArray alloc] init];;
for(int i = 0; i < [_combinedBirthdates count]; i++)
{
NSString *date = [_combinedBirthdates objectAtIndex:i];
NSArray *dateComponents = [date componentsSeparatedByString:@"/"];
if([dateComponents count] == 3)
{
[newBirthDates addObject:[NSString stringWithFormat:@"%@/%@",[dateComponents objectAtIndex:0], [dateComponents objectAtIndex:1]]];
}
else
{
[newBirthDates addObject:date];
}
}
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];
for (int i=0;i<newBirthDates.count;i++)
{
NSDate *date =[Formatter1 dateFromString:[newBirthDates objectAtIndex:i ]];
NSComparisonResult result = [date compare:todaydate];
if(result == NSOrderedSame)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date;
localNotif.alertBody = @"birthdate notification";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}