Skip to content

Instantly share code, notes, and snippets.

@lukecurtis93
Created January 25, 2018 11:33
Show Gist options
  • Save lukecurtis93/2f79763e8324ce253d206a43ec8ad467 to your computer and use it in GitHub Desktop.
Save lukecurtis93/2f79763e8324ce253d206a43ec8ad467 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
import XCPlayground
import PlaygroundSupport
import UIKit
struct Industry: Codable {
let name: String
let id: Int
func getString() {
print( "Name: \(name), Id: \(id)" )
}
}
var industries = [Int: Industry]()
do {
if let file = URL(string: "http://joben.test/industry") {
let data = try Data(contentsOf: file)
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let object = json as? [String: Any] {
// json is a dictionary
print(object)
} else if let object = json as? [Any] {
// json is an array
for anItem in object as! [Dictionary<String, AnyObject>] {
let industryName = anItem["name"] as! String
let industryID = anItem["id"] as! Int
let industry = Industry(name: industryName, id:industryID)
industries[industryID] = industry
industries[industryID]?.getString()
}
} else {
print("JSON is invalid")
}
} else {
print("no file")
}
} catch {
print(error.localizedDescription)
}
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment