gpt4 book ai didi

ios - stringWithFormat、dateFromString 和 stringFromDate 不起作用

转载 作者:行者123 更新时间:2023-11-29 03:40:12 25 4
gpt4 key购买 nike

我正在尝试以 dd:hh:mm:ss< 形式在 IBOutlet 标签中显示 UIDatePicker 输入和现实世界时间的时间差。我的 NSLog 显示了正确的信息,但我很难在标签中显示相同的数据。截至目前,我的 UILabel 中没有显示任何内容,但正确的数据位于 NSLog 中。当我改变时

[self.formatter stringFromDate:timeDif];

[self.formatter stringFromDate:selected];

显示当前DatePicker选择时间(这不是我想要的),但NSLog仍然正确显示时间差。我知道我的格式化顺序有问题,但我无法看到哪里出了问题。

- (IBAction)displayDate:(id)sender {

NSDate *timeDif = [self.formatter dateFromString:self.string];
NSDate *todaysDate = [NSDate date];
NSDate *selected = [picker date];

self.formatter = [NSDateFormatter new];
[self.formatter setDateFormat:@"dd:hh:mm:ss"];

[picker setDate:selected animated:YES];

self.label.text = [self.formatter stringFromDate:timeDif];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags
fromDate:todaysDate
toDate:selected
options:NSWrapCalendarComponents];

NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

dateComponents.day = days;
dateComponents.hour = hours;
dateComponents.minute = minutes;
dateComponents.second = seconds;

self.string = [NSString stringWithFormat:@"%1d:%1d:%1d:%1d",
dateComparisonComponents.day,
dateComparisonComponents.hour,
dateComparisonComponents.minute,
dateComparisonComponents.second];

NSLog(@"%1d:%1d:%1d:%1d",
dateComparisonComponents.day,
dateComparisonComponents.hour,
dateComparisonComponents.minute,
dateComparisonComponents.second);
}

最佳答案

您有大量额外且不必要的代码。只需这样做:

- (IBAction)displayDate:(id)sender {
NSDate *todaysDate = [NSDate date];
NSDate *selected = [picker date];

NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit |NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags
fromDate:todaysDate
toDate:selected
options:NSWrapCalendarComponents];

NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

self.label.text = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld",
days,
hours,
minutes,
seconds];
}

在您的原始代码中,第一次运行代码时甚至不会设置self.formatter。这意味着 timeDif 第一次将为 nil。您还可以获取选择器的日期,然后将选择器的日期设置为相同的值。没有理由这样做。并且您出于未知原因而使用了dateComponents。最后,self.string 的用途是什么?

关于ios - stringWithFormat、dateFromString 和 stringFromDate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18555859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com