Skip to content

Instantly share code, notes, and snippets.

Avatar

Julian Tejera-Frias juliantejera

View GitHub Profile
View FinalAlien.swift
struct Alien: Decodable {
private enum CodingKeys: String, CodingKey {
case humans
}
let humans: [Human]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
View DecodingExtension.swift
extension KeyedDecodingContainer where K : CodingKey {
func decodeArrayWithOptionalElements<T>(_ type: [T].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [T] where T : Decodable {
return (try decode([DecodableContainer<T>].self, forKey: key)).compactMap { $0.value }
}
}
extension JSONDecoder {
@juliantejera
juliantejera / Alien.swift
Created Apr 8, 2019
Alien with Decodable Container
View Alien.swift
struct Alien: Decodable {
private enum CodingKeys: String, CodingKey {
case humans
}
let humans: [Human]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
@juliantejera
juliantejera / DecodableContainer.swift
Created Apr 8, 2019
Decodable Container to parse types.
View DecodableContainer.swift
struct DecodableContainer<T>: Decodable where T: Decodable {
let value: T?
init(from decoder: Decoder) throws {
value = try? T.init(from: decoder)
}
}
@juliantejera
juliantejera / Alien.json
Created Apr 8, 2019
How an Alien is Formed...
View Alien.json
{
"humans": [
{
"age": 28,
"name": "Julian"
},
{
"age": 32,
"name": "Waldo"
},
@juliantejera
juliantejera / Models.swift
Created Apr 8, 2019
Human and Alien Models
View Models.swift
struct Human: Decodable {
let age: Int
let name: String
}
struct Alien: Decodable {
let humans: [Human]
}
@juliantejera
juliantejera / RainbowPlayground.swift
Last active Jul 1, 2017
How to draw a rainbow with Bezier Paths
View RainbowPlayground.swift
//: Playground - noun: a place where people can play
import UIKit
import Foundation
import PlaygroundSupport
extension UIColor {
class var indigo: UIColor {
return UIColor(colorLiteralRed: 75.0/255.0, green: 0, blue: 130.0/255.0, alpha: 1)
View RepositoryParserLiaison.swift
//
// RepositoryParserLiaison.swift
// Fire
//
// Created by Julian Tejera on 6/13/15.
// Copyright (c) 2015 Julian Tejera. All rights reserved.
//
import Foundation
@juliantejera
juliantejera / Prime.swift
Created Aug 10, 2016
Sieve of Erastosthene
View Prime.swift
func sieveOfErastosthene(max: Int) -> [Bool] {
// we assume every number is prime
var flags = Array(count: max + 1, repeatedValue: true)
flags[0] = false
flags[1] = false
let squareRoot = Int(sqrt(Double(max)))
for i in 2...squareRoot {
if flags[i] {
View Product.swift
let context = CoreDataManager.sharedInstance.managedObjectContext
let product = Product(context: context)
// modificas el producto como deseas
func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
CoreDataManager.sharedInstance.managedObjectContext.rollback()
}
You can’t perform that action at this time.