Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我们如何将日期设置为带有字符串的日期选择器,格式为12:30 AM?_Iphone_Objective C - Fatal编程技术网

Iphone 我们如何将日期设置为带有字符串的日期选择器,格式为12:30 AM?

Iphone 我们如何将日期设置为带有字符串的日期选择器,格式为12:30 AM?,iphone,objective-c,Iphone,Objective C,大家好,我想将日期设置为datepicker,格式如问题中给出的字符串 我使用以下代码将日期设置为datepicker NSString *tmStr=[selectedDics objectForKey:@"Time"]; NSArray *timeStr=[tmStr componentsSeparatedByString:@" "]; NSString *fStr=[timeStr objectAtIndex:1]; NSArray *Time=[[timeStr objectAtInde

大家好,我想将日期设置为datepicker,格式如问题中给出的字符串 我使用以下代码将日期设置为datepicker

NSString *tmStr=[selectedDics objectForKey:@"Time"];
NSArray *timeStr=[tmStr componentsSeparatedByString:@" "];
NSString *fStr=[timeStr objectAtIndex:1];
NSArray *Time=[[timeStr objectAtIndex:0]componentsSeparatedByString:@":"];
NSInteger hour=[[Time objectAtIndex:0] intValue];

NSString *str=[NSString stringWithFormat:@"%i:%i",hour,[[Time objectAtIndex:1]intValue]];

NSString *a;
if ([fStr isEqualToString:@"PM"]) 
{
    a=@"PM";
    hour=hour+12;
}else
{
    a=@"AM";
    if (hour==12) {
        hour=0;
    }
}
NSCalendar *calender = [NSCalendar currentCalendar];
[calender setTimeZone:[NSTimeZone defaultTimeZone]];
unsigned currentFlag = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *comps = [calender components:currentFlag fromDate:[NSDate date]];
comps.hour = hour;
comps.minute = [[Time objectAtIndex:1]intValue];
datePicker.date = [calender dateFromComponents:comps];
有没有其他方法可以轻松设置

 NSDateFormatter *formatter;
 formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"h:mm a"];

 NSDate *date=[formatter dateFromString:@"12:30 AM"]; 
 [datepicker setDate:date];