Created
December 28, 2018 10:47
-
-
Save JayachandraA/c26971a3ee445615b0692b3decbb597b to your computer and use it in GitHub Desktop.
Sample code snippet to read local JSON file in iOS10 and iOS 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let resource = Bundle.main.url(forResource: "US zip (postal) codes", withExtension: "json"), | |
let postalCodesData = try? Data(contentsOf: resource, options: Data.ReadingOptions.alwaysMapped){ | |
do{ | |
let decodedObject = try JSONDecoder().decode(PostalCodeResponse.self, from: postalCodesData) | |
DispatchQueue.main.async { [weak self] in | |
// update UI | |
} | |
}catch let parsingError { | |
print(parsingError) | |
} | |
} | |
//helpers | |
struct PostalCode: Codable { | |
var city: String | |
var loc: [Double] | |
var pop: Int | |
var state: String | |
var _id: String | |
} | |
struct PostalCodeResponse: Codable { | |
var codes: [PostalCode] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment