Skip to content

Instantly share code, notes, and snippets.

@nonafiona
Last active September 18, 2017 21:25
Show Gist options
  • Save nonafiona/499e0cd4db4e2ff8c6cb8bf9aa9c4186 to your computer and use it in GitHub Desktop.
Save nonafiona/499e0cd4db4e2ff8c6cb8bf9aa9c4186 to your computer and use it in GitHub Desktop.
y'all I'm getting a "nil" response with swift 4's new decodable
//: Playground - noun: a place where people can play
import Foundation
import PlaygroundSupport
// Struct
struct Listing {
let name: String
let city: String
let bedrooms: Int
let price: Int
let thumbnail: URL
let id: Int
init(name: String, city: String, bedrooms: Int, price: Int, thumbnail: URL, id: Int) {
self.name = name
self.city = city
self.bedrooms = bedrooms
self.price = price
self.thumbnail = thumbnail
self.id = id
}
}
extension Listing: Decodable {
// declaring our keys
enum Keys: String, CodingKey {
case listing = "listing"
case pricing = "pricing_quote"
}
enum ListingKeys: String, CodingKey {
case name = "name"
case city = "city"
case bedrooms = "bedrooms"
case id = "id"
case thumbnail = "picture_url"
}
enum PriceKeys: String, CodingKey {
case nightly_price = "localized_nightly_price"
}
init(from decoder: Decoder) throws {
// defining our keyed container
let container = try decoder.container(keyedBy: Keys.self)
// Listing Keys - contains all info about listing
let listingContainer = try container.nestedContainer(keyedBy: ListingKeys.self, forKey: .listing)
// Info within the Listing Keys -
let name: String = (try listingContainer.decodeIfPresent(String.self, forKey: .name))!
let city: String = (try listingContainer.decodeIfPresent(String.self, forKey: .city))!
let bedrooms: Int = (try listingContainer.decodeIfPresent(Int.self, forKey: .bedrooms))!
let id: Int = (try listingContainer.decodeIfPresent(Int.self, forKey: .id))!
let thumbnail: URL = (try listingContainer.decodeIfPresent(URL.self, forKey: .thumbnail))!
// Price Keys - contains info about nightly price
let priceContainer = try container.nestedContainer(keyedBy: PriceKeys.self, forKey: .pricing)
// Info within Price Keys -
let price: Int = (try priceContainer.decodeIfPresent(Int.self, forKey: .nightly_price))!
self.init(name: name, city: city, bedrooms: bedrooms, price: price, thumbnail: thumbnail, id: id)
}
}
struct RentalList : Decodable {
let data: [Listing]
}
typealias JSON = [String: Any]
// Network Request
enum NetworkError: Error {
case unknown
case couldNotParseJSON
}
class Networking {
let session = URLSession.shared
let url = URL(string:"https://api.airbnb.com/v2/search_results?key=915pw2pnf4h1aiguhph5gc5b2")!
func getListing(id: Int, completion: @escaping ([Listing]) -> Void) {
let request = URLRequest(url: url)
session.dataTask(with: request) { (data, resp, err) in
if let data = data {
let rentalList = try? JSONDecoder().decode(RentalList.self, from: data) // returning nil instead of JSON object
guard let listings = rentalList?.data else {return}
completion(listings)
}
}.resume()
}
}
// return a listing
let networking = Networking()
networking.getListing(id: 861986, completion: { (res) in
print(res)
})
// Error Handling
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment