Skip to content

Instantly share code, notes, and snippets.

View Ravi61's full-sized avatar

Ravi Kumar Aggarwal Ravi61

View GitHub Profile
@Ravi61
Ravi61 / Dictionary.json
Created July 13, 2017 18:33
Parsing JSON as Dictionary
{
"Death Star": {
"model": "DS-1 Orbital Battle Station",
"movie": "Star Wars: Episode VI – Return of the Jedi"
},
"Millennium Falcon": {
"model": "YT-1300 light freighter",
"movie": "Star Wars: Episode V – The Empire Strikes Back"
}
}
@Ravi61
Ravi61 / DecodingError.swift
Created July 13, 2017 15:30
Decoding Error Catch
//...
do {
let model = try jsonDecoder.decode(BattleShip.self, from: jsonData!)
print(model)
} catch DecodingError.dataCorrupted(let context) {
print(context.debugDescription)
} catch DecodingError.keyNotFound(let key, let context) {
print("\(key.stringValue) was not found, \(context.debugDescription)")
} catch DecodingError.typeMismatch(let type, let context) {
print("\(type) was expected, \(context.debugDescription)")
@Ravi61
Ravi61 / RootLevel.json
Created July 13, 2017 15:20
Root level wrapped JSON
[
{
"name": "Star Wars: Episode IV – A New Hope",
"director": "George Lucas"
},
{
"name": "Star Wars: Episode VI – Return of the Jedi",
"director": "Richard Marquand"
}
]
@Ravi61
Ravi61 / CustomDateCoding.swift
Created July 13, 2017 15:16
Custom Date Encoding/Decoding
// Custom Date Decoding
jsonDecoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let data = try decoder.singleValueContainer().decode(String.self)
//perform your operation on obtained string
let disturbance = "01-07-1977"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
if data == "First disturbance in force" {
return formatter.date(from: disturbance) ?? Date()
} else {
@Ravi61
Ravi61 / CustomEncoder.swift
Created July 13, 2017 12:27
Custom Encoding Code
struct BattleShip {
var name: String
var created: String
var movieName: String
var director: String
enum CodingKeys: String, CodingKey {
case name
case created
case film
@Ravi61
Ravi61 / CustomCoder.json
Created July 13, 2017 12:25
JSON for custom Encoding/Decoding
{
"name": "Death Star",
"created": "2014-12-10T16:36:50.509000Z",
"film": {
"name": "Star Wars: Episode IV – A New Hope",
"director": "George Lucas"
}
}
@Ravi61
Ravi61 / Floating.swift
Created July 12, 2017 19:20
Encoding/Decoding strategy to handle Non Conforming Floats
decoder.nonConformingFloatDecodingStrategy = .convertFromString(
positiveInfinity: "+Infinity",
negativeInfinity: "-Infinity",
nan: "NaN"
)
@Ravi61
Ravi61 / BattleshipURL.swift
Last active July 12, 2017 19:37
Model with URL
struct BattleShip: Codable {
//...
var url: URL
//...
}
@Ravi61
Ravi61 / BattleshipDate.swift
Last active July 13, 2017 17:43
Battleship model for encoding/decoding dates
struct BattleShip: Codable {
//...
var created: Date
//...
}
//...
// Let's decode
let decoder = JSONDecoder()
do {
@Ravi61
Ravi61 / RootDecode.swift
Created July 12, 2017 18:18
Decoding root level array
let decoder = JSONDecoder()
do {
let films = try decoder.decode([Film].self, from: jsonData)
} catch {
print("parse error")
}