NSDateFormatter 在样式和格式之间交换

NSDateFormatter swapping between Style and Format

使用相同的 NSDateFormatter 并在样式和格式定义之间交换是否安全?例如:

NSDateFormatter *df = [NSDateFormatter new];
df.dateStyle = NSDateFormatterMediumStyle;
df.timeStyle = NSDateFormatterMediumStyle;
NSLog(@"Medium Style / Medium Time: %@", [df stringFromDate:[NSDate date]]);
df.dateFormat = @"MMM/d/yy hh:mm:ss";
NSLog(@"Custom Format: %@", [df stringFromDate:[NSDate date]]);
df.dateStyle = NSDateFormatterFullStyle;
df.timeStyle = NSDateFormatterNoStyle;
NSLog(@"Full Style/ No Time: %@", [df stringFromDate:[NSDate date]]); 
// ^-- how does it know to stop using the format I set before?

在最后一行,NSDateFormatter 是如何知道停止使用我之前指定的日期格式的?这样做安全可靠吗?

如果 formatnil,则日期格式化程序使用 style。所以添加以下行:

df.dateFormat = nil;

当您想返回使用style