By using a simple arrow operator that takes care of the boilerplate code for us. Json mapping code becomes concise and maintainable ❤️
Why use Arrow
Infers types
Leaves your models clean
Handles custom & nested models
Dot and array syntax
Pure Swift, Simple & Lightweight
Example
Swift Model
struct Profile {
var identifier = 0
var name = ""
var link:NSURL?
var weekday:WeekDay = .Monday
var stats = Stats()
var phoneNumbers = [PhoneNumber]()
}
var profile = Profile()
// Int
if let id = json["id"] as? Int {
profile.identifier = id
}
// String
if let name = json["name"] as? String {
profile.name = name
}
// NSURL
if let link = json["link"] as? String, url = NSURL(string:link) {
profile.link = link
}
// Enum
if let weekdayInt = json["weekdayInt"] as? Int, weekday = WeekDay(rawValue:weekdayInt) {
profile.weekday = weekday
}
// Custom nested object
if let statsJson = json["stats"] as? AnyObject {
if let numberOfFans = statsJson["numberOfFans"] as? Int {
profile.stats.numberOfFans = numberOfFans
}
if let numberOfFriends = statsJson["numberOfFriends"] as? Int {
profile.stats.numberOfFriends = numberOfFriends
}
}
// Array of custom nested object
if let pns = json["phoneNumbers"] as? [AnyObject] {
for pn in pns {
phoneNumbers.append(PhoneNumber(json: pn))
}
}
The Swift Package Manager (SPM) is now the official way to install Arrow. The other package managers are now deprecated as of 5.1.2 and won’t be supported in future versions.
// Configure Global Date Parsing with one of those
Arrow.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")
Arrow.setUseTimeIntervalSinceReferenceDate(true)
Arrow.setDateFormatter(aDateFormatter)
// Then later dates can be parsed form custom date format or timestamps automatically 🎉
let json:JSON = JSON(["date": "2013-06-07T16:38:40+02:00", "timestamp": 392308720])
date1 <-- json["date"]
date2 <-- json["timestamp"]
Arrow
Reason - Example - Installation
Because parsing JSON in Swift is full of unecessary if lets, obvious casts and nil-checks
There must be a better way
Try it
Arrow is part of freshOS iOS toolset. Try it in an example App! Download Starter Project
How
By using a simple arrow operator that takes care of the boilerplate code for us.
Json mapping code becomes concise and maintainable ❤️
Why use Arrow
Example
Swift Model
JSON File
Before (Chaos)
After 🎉🎉🎉
Usage
Installation
The Swift Package Manager (SPM) is now the official way to install
Arrow
. The other package managers are now deprecated as of5.1.2
and won’t be supported in future versions.Swift Package Manager
Xcode
>File
>Swift Packages
>Add Package Dependency...
>Paste
https://github.com/freshOS/Arrow
Carthage - Deprecated
CocoaPods - Deprecated
How Does That Work
Notice earlier we typed :
That’s because we created and extension “Stats+Arrow.swift” enabling us to use the Arrow Operator
Flexible you said
Date Parsing
Globally
On a per-key basis
Just provide it on a case per case basis ! 🎉
Accessing JSON values
Nested values
Object at index
Combine both
Looping on Array
Swift Version
Acknowledgements
This wouldn’t exist without YannickDot, Damien-nd and maxkonovalov
Backers
Like the project? Offer coffee or support us with a monthly donation and help us continue our activities :)
Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site :)