Date 中的字符串不起作用
String from Date not working
-(void)viewLogAction
{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:_selectedDate];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *convertedString = [formatter stringFromDate:date];
_allFood = [CoreDataMethods foodLogFromCoreData:convertedString];
if([_allFood count]==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No data" message:@"Sorry, No food item to display on this date" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
[self performSegueWithIdentifier:@"viewLogSegue" sender:self];
}
}
这是我的代码。 NSString _selectedDate
具有值 2017-05-09
,但行 NSDate *date = [formatter dateFromString:_selectedDate];
未将任何值传递给 date
。请帮我找出问题所在。
试试这个
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:_selectedDate];
[formatter setDateFormat:@"dd-MM-yyyy"];
您发布的代码很好,如果 _selectedDate
不为零,则可以正常工作。检查您是否正确设置了 _selectedDate
中的值。
-(void)viewLogAction
{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:_selectedDate];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *convertedString = [formatter stringFromDate:date];
_allFood = [CoreDataMethods foodLogFromCoreData:convertedString];
if([_allFood count]==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No data" message:@"Sorry, No food item to display on this date" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
[self performSegueWithIdentifier:@"viewLogSegue" sender:self];
}
}
这是我的代码。 NSString _selectedDate
具有值 2017-05-09
,但行 NSDate *date = [formatter dateFromString:_selectedDate];
未将任何值传递给 date
。请帮我找出问题所在。
试试这个
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:_selectedDate];
[formatter setDateFormat:@"dd-MM-yyyy"];
您发布的代码很好,如果 _selectedDate
不为零,则可以正常工作。检查您是否正确设置了 _selectedDate
中的值。