Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
View GitHub Profile
@ts95
ts95 / JSONDecodingExample.swift
Created October 21, 2017 07:15
JSON Decoding Example
let json = """
{
"isbn": "95-0443-843-0",
"title": "Do Androids Dream of Electric Sheep?",
"authors": ["Philip K. Dick"],
"year": 1968,
"country": "United States"
}
""".data(using: .utf8)!
@ts95
ts95 / Book.swift
Last active October 21, 2017 07:05
Book.swift
struct Book: Codable {
var isbn: String
var title: String
var authors: [String]
var year: Int
}
import FirebaseFirestore
private struct Property {
let label: String
let value: Any
}
struct FirestoreModelData {
let snapshot: DocumentSnapshot
let book = FirestoreBook(
documentID: nil,
isbn: "95-0443-843-0",
title: "Do Androids Dream of Electric Sheep?")
let firestore = Firestore.firestore()
let ref = firestore.collection("books").document()
ref.setModel(book)
struct FirestoreBook {
let documentID: String!
var isbn: String
var title: String
}
extension FirestoreBook: FirestoreModel {
init?(modelData: FirestoreModelData) {
try? self.init(
let firestore = Firestore.firestore()
let ref = firestore.document("books/95-0443-843-0")
ref.getModel(FirestoreBook.self) { book, error in
guard let book = book else { return }
print("The book \(book.title) was published in \(book.year).")
}
let book = FirestoreBook(
isbn: "95-0443-843-0",
title: "Do Androids Dream of Electric Sheep?",
authors: ["Philip K. Dick"],
year: 1968,
country: "United States",
createdAt: Date(),
format: .ebook,
award: nil)
struct FirestoreBook {
var isbn: String
var title: String
var authors: [String]
var year: Int
var country: String
var createdAt: Date
var format: Format
var award: String?
@ts95
ts95 / Book.swift
Last active October 5, 2017 06:25
struct Book {
var isbn: String
var title: String
var authors: [String]
var year: Int
var country: String
var createdAt: Date
var format: Format
var award: String?
import UIKit
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(describing: self)