Formatter
Formatter is an abstract class that declares an interface for an object that creates, converts, or validates a human readable form of some data. The types that subclass the Formatter
class are generally used when we want to take a particular object, such as an instance of the Date
class, and present the value in a form that the user of our application can understand.
Apple has provided several concrete implementations of the Formatter
class, and in this section we will look at two of them. It is important to remember that the formatters that Apple provides will provide the proper format for the default locale of the device the application is running on.
We will start off by looking at the DateFormatter
type.
DateFormatter
The DateFormatter
class is a subclass of the Formatter
abstract class that can be used to convert a Date
object into a human-readable string. It can also be used to convert a String
representation of a date into a Date
object. We will look at both of these use cases...