I recently found out about the new-to-iOS8 class NSDateComponentsFormatter, which lets you format time intervals rather than dates. Cool. I've written code to do this more times than I care to think about. So I decided to try and use it in a playground, but I can't get it to work. Here's my (Swift) code:
var componentsFormatter = NSDateComponentsFormatter()
componentsFormatter.allowedUnits =
.CalendarUnitHour |
.CalendarUnitMinute |
.CalendarUnitSecond
componentsFormatter.unitsStyle = .Positional
let interval: NSTimeInterval = 345.7
let intervalstring = componentsFormatter.stringFromTimeInterval(interval)
println("Interval as String = \(intervalstring)")
This displays
Interval as String = nil
I've tried various things, but no joy. Does anybody have a working example using this new class, or can you spot what I'm missing?
(I speak Objective-C too, so if you have sample code in Objective-C that works as well.)
Swift 3.1 • Xcode 8.3.2
Positional Time