Skip to content

Transformations

Brian Mullen edited this page Dec 4, 2016 · 2 revisions

What if you don't like how Outlaw implements the default value extraction? Or you have an edge case for extraction?

No problem! All you need is a transformation function when extracting the values. Outlaw will extract the raw value for you and supply it to your transformation function.

Example

let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "GMT")
formatter.dateFormat = "MM/dd/yyyy"

let date: Date? = json.value(for: "date", with: { (rawValue: String) -> Date? in
     return formatter.date(from: rawValue)
})

We can also use the power of swift and simplify the above code to:

let date: Date? = json.value(for: "date", with: formatter.date)

Clone this wiki locally