Created
June 26, 2017 18:45
-
-
Save shepting/a156d97be2d38dad0b79f5d59ccfeae2 to your computer and use it in GitHub Desktop.
Swift Networking for RandomUser.API
This file contains 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
// | |
// JSONParsing.swift | |
// DemoPersonViewer | |
// | |
// Created by Steven Hepting on 6/26/17. | |
// Copyright © 2017 Hepting. All rights reserved. | |
// | |
import Foundation | |
typealias JSONDict = [String: Any] | |
extension Dictionary where Key == String { | |
func string(_ key: String) -> String? { | |
if let value = self[key] as? String { | |
return value | |
} else { | |
print("Missing parameter '\(key)'") | |
return nil | |
} | |
} | |
func array(_ key: String) -> [JSONDict] { | |
if let value = self[key] as? [JSONDict] { | |
return value | |
} else { | |
print("Missing parameter '\(key)'") | |
return [] | |
} | |
} | |
func dict(_ key: String) -> JSONDict? { | |
if let value = self[key] as? JSONDict{ | |
return value | |
} else { | |
print("Missing parameter '\(key)'") | |
return nil | |
} | |
} | |
} | |
This file contains 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
// | |
// Networking.swift | |
// DemoPersonViewer | |
// | |
// Created by Steven Hepting on 6/23/17. | |
// Copyright © 2017 Hepting. All rights reserved. | |
// | |
import Foundation | |
func parseUsersJSON(from data: Data) -> [User] { | |
do { | |
if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] { | |
var users = [User]() | |
for userJSON in json.array("results") { | |
if let user = User(json: userJSON) { | |
users.append(user) | |
} | |
} | |
return users | |
} | |
} catch { | |
print("Couldn't parse JSON: \(error)") | |
if let utf8String = String(data: data, encoding: String.Encoding.utf8) { | |
print("Received: \(utf8String)") | |
} | |
} | |
return [] | |
} | |
struct Client { | |
func loadUsers(completion: @escaping ([User], Error?) -> ()) { | |
let url = URL(string: "https://randomuser.me/api?results=20")! | |
let task = URLSession.shared.dataTask(with: url) {data, response, error in | |
var users = [User]() | |
if let data = data { | |
users = parseUsersJSON(from: data) | |
} | |
if let error = error { | |
print(error) | |
} | |
completion(users, error) | |
} | |
task.resume() | |
} | |
} |
This file contains 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
// | |
// User.swift | |
// DemoPersonViewer | |
// | |
// Created by Steven Hepting on 6/26/17. | |
// Copyright © 2017 Hepting. All rights reserved. | |
// | |
import Foundation | |
struct User { | |
let first: String | |
let last: String | |
let thumbnail: URL | |
// let fullProfile: URL | |
init?(json: [String: Any]) { | |
guard let name = json.dict("name") else { return nil } | |
guard let first = name.string("first") else { return nil } | |
guard let last = name.string("last") else { return nil } | |
guard let thumbnail = json.dict("picture")?.string("thumbnail") else { return nil } | |
guard let thumbnailURL = URL(string: thumbnail) else { return nil } | |
self.first = first | |
self.last = last | |
self.thumbnail = thumbnailURL | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment