-(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
。請幫我找出問題所在。String from Date not working
u分配。 ** formatter = [[NSDateFormatter alloc] init]; **兩次刪除第二次它將工作 –
你調試和檢查日期是否爲零? –
代碼運行良好。確保_選定的日期格式正確 –