Skip to content

Instantly share code, notes, and snippets.

View candostdagdeviren's full-sized avatar

Candost candostdagdeviren

View GitHub Profile
final class User: Model {
init(node: Node, in context: Context) throws {
id = try node.extract("_id") // Trick for MongoDB database
name = try node.extract("name")
score = try node.extract("score")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"_id": id, // Trick for MongoDB database
final class User: Model {
let storage = Storage()
var name: String
var score: Int
init(row: Row) throws {
name = try row.get("name")
score = try row.get("score")
}
func makeRow() throws -> Row {
extension User: JSONRepresentable {
func makeJSON() throws -> JSON {
var json = JSON()
try json.set("id", id?.string)
try json.set("name", name)
try json.set("score", score)
return json
}
}
class OurCoolCollection: RouteCollection {
typealias Wrapped = HTTP.Responder
func build<Builder: RouteBuilder>(_ builder: Builder) where Builder.Value == Responder { ... }
}
class OurCoolCollection: RouteCollection {
func build(_ builder: RouteBuilder) throws { ... }
}
@candostdagdeviren
candostdagdeviren / links.md
Created June 14, 2016 08:21 — forked from MarkVillacampa/links.md
All the Apple Developer links you need from WWDC16
@candostdagdeviren
candostdagdeviren / ViewRenderable.swift
Last active August 9, 2018 08:10
Custom View Implementation for iOS
import UIKit
protocol ViewCustomized {
func createView<T>(_ viewClass: T.Type, parentView: UIView) -> T where T: UIView, T: ViewRenderable
}
protocol ViewRenderable {
func render()
}
@candostdagdeviren
candostdagdeviren / Concurrency3.swift
Last active November 28, 2018 10:09
Swift Post Concurrency Blog Post Code Example
class FetchRestaurantInventoryOperation: Operation {
var restaurantData: RestaurantData
var networkProvider: NetworkProvider
// 1
private var _executing = false {
willSet {
willChangeValue(forKey: "isExecuting")
}
didSet {
@candostdagdeviren
candostdagdeviren / Concurrency1.swift
Last active November 29, 2018 14:09
Swift Post Concurrency Blog Post Code Example
let url = URL(string: "https://theswiftpost.co/")
if let url = url {
showLoadingIndicator() // UI Operation; has to run on the main queue
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
showErrorAlert(error) // UI operation; has to run on the main queue!!
} else if let data = data {
debugPrint(usableData) //JSONSerialization
}
debugPrint(“Completed”)
@candostdagdeviren
candostdagdeviren / Concurrency2.swift
Last active November 29, 2018 14:15
Swift Post Concurrency Blog Post Code Example
/* I put the first DispatchQueue method intentionally to indicate that we're on
the background queue. Just to support the explanation for beginner developers
This is not necessary in real-life, URLSession is thread-safe. Whenever you call URLSession
it'll start executing on background. */
DispatchQueue.global().async {
let url = URL(string: "https://theswiftpost.co/")
if let url = url {
DispatchQueue.main.sync {
showLoadingIndicator() // UI Operation; has to run on the main queue
}