将字符串转换为时间xcode

时间:2016-06-26 18:27:47

标签: ios objective-c xcode nsstring nsdate

我有一个包含不同时间的json。如下所示

{ " 5:10" " 14:24" } 我需要利用这段时间每天发出本地通知。

您能否建议我如何正确地将此字符串放入此行?

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit |   NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setHour: 20] ;
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]  init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;

NSLog(@"notification: %@",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

在此代码中,我手动将时间添加到晚上8点。我需要把时间从json放到代码中。

0 个答案:

没有答案