Protocol
ParseStrategy
protocol ParseStrategy : Decodable, Encodable, Hashable
Overview
A ParseStrategy allows you to convert a formatted representation into a data type, using one of two approaches:
Initialize the data type by calling an initializer of that type that takes a formatted instance and a parse strategy as parameters. For example, you can create a
Decimalfrom a formatted string with the initializerDecimal/init(_:format:lenient:)-6fk71.Create a parse strategy and call its
parse(_:)method on one or more formatted instances.
ParseStrategy is closely related to FormatStyle, which provides the opposite conversion: from data type to formatted representation. To use a parse strategy, you create a FormatStyle to define the representation you expect, then access the style’s parseStrategy property to get a strategy instance.
The following example creates a Decimal.FormatStyle.Currency format style that uses US dollars and US English number-formatting conventions. It then creates a Decimal instance by providing a formatted string to parse and the format style’s parseStrategy.
let style = Decimal.FormatStyle.Currency(code: "USD",
locale: Locale(identifier: "en_US"))
let parsed = try? Decimal("$12,345.67",
strategy: style.parseStrategy) // 12345.67
Topics
Associated Types
associatedtype ParseInputThe input type parsed by this strategy.associatedtype ParseOutputThe output type returned by this strategy.
Instance Methods
func parse(Self.ParseInput) throws -> Self.ParseOutputParses a value, using this strategy.
Type Properties
static var dateTime: Date.FormatStyleA default format style for formatting dates.static var http: DateComponents.HTTPFormatStylestatic var http: Date.HTTPFormatStylestatic var iso8601: Date.ISO8601FormatStyleA style for formatting a date in accordance with the ISO-8601 standard.static var iso8601: DateComponents.ISO8601FormatStyle
Type Methods
static func fixed(format: Date.FormatString, timeZone: TimeZone, locale: Locale?) -> SelfA fixed-format date parse strategy.
Relationships
Inherits From
Swift.DecodableSwift.EncodableSwift.EquatableSwift.Hashable