As per Apple Documentation, the definition of Formatter is “An abstract class that declares an interface for objects that create, interpret, and validate the textual representation of values”. We have already seen the most commonly used Formatter like DateFormatter
and NumberFormatter
. In this blog, we will look into some lesser-known Built-in Formatter.
1) MeasurementFormatter => It availability is from iOS 10. It provides localized representations of units and measurements. Let see an example of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
func mesurementFormatter(){ // Converting one unit to another unit. let measurementFormatter = MeasurementFormatter() let speedInMiles = Measurement<UnitSpeed>(value: 100, unit: .milesPerHour) measurementFormatter.unitOptions = [.providedUnit] measurementFormatter.unitStyle = .medium // Unit style (.long, .medium,.short) print(measurementFormatter.string(from: speedInMiles.converted(to: .kilometersPerHour))) // Convert Miles per hour to Kilometer per hour ==> output(160.934 km/h) measurementFormatter.numberFormatter.usesSignificantDigits = true measurementFormatter.numberFormatter.maximumSignificantDigits = 5 print(measurementFormatter.string(from: speedInMiles.converted(to: .kilometersPerHour))) // Output(160.93 km/h) //Converting Distance, kilometer to meter let distanceInMiles = Measurement<UnitLength>(value: 100, unit: .kilometers) measurementFormatter.unitStyle = .long // Unit style (.long, .medium,.short) print(measurementFormatter.string(from: distanceInMiles.converted(to: .meters))) // Output(100,000 meters) } |
In the above example, we have created the measurementFormatter method and after that, we have called it from viewDidLoad.
2) PersonNameComponentsFormatter => It availability is from iOS 9.0+. It provides localized representations of the components of a person’s name. So, this formatter will be useful whenever we have to parse and format a person’s name. Now we will see how to use this formatter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func personNameFormatter(){ let personFormatter = PersonNameComponentsFormatter() let components = personFormatter.personNameComponents(from: "Sir Richard Henry Kater") print(personFormatter.personNameComponents(from: "Sir Richard Henry Kater") ?? "") // Output (namePrefix: Sir givenName: Richard middleName: Henry familyName: Kater) print(components?.familyName ?? "")// Output(Kater) print(components?.givenName ?? "")// Output(Richard) print(components?.middleName ?? "")// Output(Henry) print(components?.namePrefix ?? "")// Output(Sir) //You cam also set different style for the name personFormatter.style = .abbreviated // Style (.abbreviated, .default, .long, .medium,.short) print(personFormatter.string(from: components!)) // Output (Rk) } |
Similarly, like measurementFormatter method, we have created an example method of personNameformatter. And called it from viewDidLoad.
3) RelativeDateTimeFormatter => It creates locale-aware string representations of a relative date or time. Let see an example of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func relativeDateTimeFormatter(){ let relativeDateTimeFormatter = RelativeDateTimeFormatter() relativeDateTimeFormatter.unitsStyle = .spellOut //(.abbreviated, .full, .short, .spellOut) print(relativeDateTimeFormatter.localizedString(fromTimeInterval: 360)) //Output(in six minutes) print(relativeDateTimeFormatter.localizedString(fromTimeInterval: -360)) // Output (six minutes ago) //Find difference between two dates let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/mm/yyyy" if let someDate = dateFormatter.date(from: "21/01/2021"){ print("21/01/2021 was \(relativeDateTimeFormatter.localizedString(for: someDate, relativeTo: Date()))") //Output(21/01/2021 was five months ago) } } |
4) ByteCountFormatter => It converts a byte count value into a localized description that is set up with the appropriate byte modifier. Therefore it is useful whenever we need byte value into localized human readable form.
1 2 3 4 5 6 7 |
func byteCountFormatter(){ let byteCountFormatter = ByteCountFormatter() // Allowed unit .useAll means it can use any unit in the formatter content byteCountFormatter.allowedUnits = .useAll print(byteCountFormatter.string(fromByteCount: 1024 * 1024)) //Output (1 MB) } |
Thanks for reading this blog.
You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.