SwiftyJSON
SwiftyJSON makes it easy to deal with JSON data in Swift.
Platform | Build Status |
---|---|
*OS | |
Linux |
Why is the typical JSON handling in Swift NOT good?
Swift is very strict about types. But although explicit typing is good for saving us from mistakes, it becomes painful when dealing with JSON and other areas that are, by nature, implicit about types.
Take the Twitter API for example. Say we want to retrieve a user's "name" value of some tweet in Swift (according to Twitter's API).
The code would look like this:
It's not good.
Even if we use optional chaining, it would be messy:
An unreadable mess--for something that should really be simple!
With SwiftyJSON all you have to do is:
And don't worry about the Optional Wrapping thing. It's done for you automatically.
Requirements
- iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
- Xcode 8
Integration
CocoaPods (iOS 8+, OS X 10.9+)
You can use CocoaPods to install SwiftyJSON
by adding it to your Podfile
:
Carthage (iOS 8+, OS X 10.9+)
You can use Carthage to install SwiftyJSON
by adding it to your Cartfile
:
github "SwiftyJSON/SwiftyJSON" ~> 4.0
If you use Carthage to build your dependencies, make sure you have added SwiftyJSON.framework
to the "Linked Frameworks and Libraries" section of your target, and have included them in your Carthage framework copying build phase.
Swift Package Manager
You can use The Swift Package Manager to install SwiftyJSON
by adding the proper description to your Package.swift
file:
Then run swift build
whenever you get prepared.
Manually (iOS 7+, OS X 10.9+)
To use this library in your project manually you may:
- for Projects, just drag SwiftyJSON.swift to the project tree
- for Workspaces, include the whole SwiftyJSON.xcodeproj
Usage
Initialization
Or
Or
Subscript
Loop
The first element is always a String, even if the JSON is an Array
Error
SwiftyJSON 4.x
SwiftyJSON 4.x introduces an enum type called SwiftyJSONError
, which includes unsupportedType
, indexOutOfBounds
, elementTooDeep
, wrongType
, notExist
and invalidJSON
, at the same time, ErrorDomain
are being replaced by SwiftyJSONError.errorDomain
.
Note: Those old error types are deprecated in SwiftyJSON 4.x and will be removed in the future release.
SwiftyJSON 3.x
Use a subscript to get/set a value in an Array or Dictionary
If the JSON is:
- an array, the app may crash with "index out-of-bounds."
- a dictionary, it will be assigned to
nil
without a reason. - not an array or a dictionary, the app may crash with an "unrecognised selector" exception.
This will never happen in SwiftyJSON.
Optional getter
Non-optional getter
Non-optional getter is named xxxValue
Setter
Raw object
Existence
Literal convertibles
For more info about literal convertibles: Swift Literal Convertibles
Merging
It is possible to merge one JSON into another JSON. Merging a JSON into another JSON adds all non existing values to the original JSON which are only present in the other
JSON.
If both JSONs contain a value for the same key, mostly this value gets overwritten in the original JSON, but there are two cases where it provides some special treatment:
- In case of both values being a
JSON.Type.array
the values form the array found in theother
JSON getting appended to the original JSON's array value. - In case of both values being a
JSON.Type.dictionary
both JSON-values are getting merged the same way the encapsulating JSON is merged.
In a case where two fields in a JSON have different types, the value will get always overwritten.
There are two different fashions for merging: merge
modifies the original JSON, whereas merged
works non-destructively on a copy.
String representation
There are two options available:
- use the default Swift one
- use a custom one that will handle optionals well and represent
nil
as"null"
:
Work with Alamofire
SwiftyJSON nicely wraps the result of the Alamofire JSON response handler:
We also provide an extension of Alamofire for serializing NSData to SwiftyJSON's JSON.
See: Alamofire-SwiftyJSON
Work with Moya
SwiftyJSON parse data to JSON:
SwiftyJSON Model Generator
Tools to generate SwiftyJSON Models