While I was playing around with the
DateComponentsFormatter I noticed that it seems to ignore the
maximumUnitCount property when the
zeroFormattingBehavior is set to
import Foundation let interval: TimeInterval = 100 let formatter = DateComponentsFormatter() formatter.allowedUnits = [.hour, .minute, .second] formatter.maximumUnitCount = 2 formatter.unitsStyle = .abbreviated formatter.zeroFormattingBehavior = .pad formatter.string(from: interval)
The code above results in "0h 1m 40s", but I would expect it to print "1m 40s".
The reason I set the zeroFormattingBehavior to pad the result is, because I want it to always return two units for a better comparison when formatting multiple results, even when the interval is 0.
For example an interval of 0 should result in "0m 0s".
Is the behaviour intended and if yes how can I achieve the result I'm looking for?