Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 17 additions & 30 deletions Networking.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,45 @@
//: It will serve up the current directory, so make sure to be in the directory containing episodes.json

import UIKit
import XCPlayground
import PlaygroundSupport


typealias JSONDictionary = [String: AnyObject]
let url = URL(string: "http://localhost:8000/episodes.json")!

let url = NSURL(string: "http://localhost:8000/episodes.json")!


struct Episode {
struct Episode: Decodable {
let id: String
let title: String
}

extension Episode {
init?(dictionary: JSONDictionary) {
guard let id = dictionary["id"] as? String,
title = dictionary["title"] as? String else { return nil }
self.id = id
self.title = title
}
}

struct Media: Decodable {}

struct Media {}


struct Resource<A> {
let url: NSURL
let parse: NSData -> A?
struct Resource<A: Decodable> {
let url: URL
let parse: (Data) -> A?
}


extension Resource {
init(url: NSURL, parseJSON: AnyObject -> A?) {
init(url: URL) {
self.url = url
self.parse = { data in
let json = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
return json.flatMap(parseJSON)
return try? JSONDecoder().decode(A.self, from: data)
}
}
}


extension Episode {
static let all = Resource<[Episode]>(url: url, parseJSON: { json in
guard let dictionaries = json as? [JSONDictionary] else { return nil }
return dictionaries.flatMap(Episode.init)
})
static let all = Resource<[Episode]>(url: url)
}


final class Webservice {
func load<A>(resource: Resource<A>, completion: (A?) -> ()) {
NSURLSession.sharedSession().dataTaskWithURL(resource.url) { data, _, _ in
func load<A>(resource: Resource<A>, completion: @escaping (A?) -> ()) {
URLSession.shared.dataTask(with: resource.url) { data, _, _ in
guard let data = data else {
completion(nil)
return
Expand All @@ -68,8 +54,9 @@ final class Webservice {
}


XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
PlaygroundPage.current.needsIndefiniteExecution = true

Webservice().load(Episode.all) { result in
print(result)
Webservice().load(resource: Episode.all) { result in
print(result ?? "")
}

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Swift Talk
## Networking

This is the code that accompanies Swift Talk Episode 1: [Networking](https://talk.objc.io/episodes/S01E01-networking)
This is the code that accompanies Swift Talk Episode 1: [Networking](https://talk.objc.io/episodes/S01E01-networking)

It has been slightly adapted to work with Xcode 9 / Swift 4.

* some syntactical and type changes
* use of new JSONDecoder(), which simplifies code lot - see [Apple documentation](https://developer.apple.com/documentation/foundation/jsondecoder)