NSArrayにNSStringとして時間を格納するオブジェクトがあります。NSDateの代わりにNSStringをたくさん表示することになったので、NSStringを選択しました。UILocalNotificationを設定しようとしています。私がやったことは:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
dateFormatter.dateFormat = @"h:mm a";
for (NSString *s in _times) {
NSDate *date = [dateFormatter dateFromString:s];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:date];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
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]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"%@", [dateFormatter stringFromDate:itemDate]);
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertAction = thePill.name;
note.alertBody = thePill.note;
note.fireDate = itemDate;
note.timeZone = [[NSCalendar currentCalendar] timeZone];
if (note.fireDate == nil) {
NSLog(@"nil firedate");
}
// note.repeatInterval = NSDayCalendarUnit;
NSData *data = [self archivePill:thePill];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:data forKey:PILL_NOTIFICATION];
note.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:note];
}
itemDate時刻をNSLogするとき、それは私が欲しいものです。メソッドの最後に、ローカル通知はその時点ではなくすぐに発生します。私の日付が適切な時刻であるため、理由がわかりません。fireDateをその日付に設定しただけだと思いましたか?ありがとう。