Skip to content

Instantly share code, notes, and snippets.

View pietrobasso's full-sized avatar

Pietro Basso pietrobasso

View GitHub Profile
@pietrobasso
pietrobasso / NotificationCenter+AddObserver.swift
Last active August 23, 2018 07:13
Wrapper around the Notification User Info Keys used to get values from the user information dictionary of keyboard notifications. - UIKonf 2017 Conference https://www.youtube.com/watch?v=GzZO5VB1xUg
//
// NotificationCenter+AddObserver.swift
//
// Created by Pietro Basso on 27/02/2018.
//
import Foundation
import UIKit
/**
@pietrobasso
pietrobasso / SnapshotTestingDSL.swift
Created September 30, 2019 05:48
SnapshotTesting DSL
import UIKit
import XCTest
import SnapshotTesting
public protocol SnapshottableView {
var snapshotView: UIView { get }
}
extension UIView: SnapshottableView {
public var snapshotView: UIView {
@pietrobasso
pietrobasso / ExtractYouTubeId.playground
Last active October 7, 2020 22:59 — forked from zdk/extract_youtube_id.mm
A Regex to extract YouTube video ids from the given list of youtube URLs in Swift 4
//: Playground - noun: a place where people can play
//
// Created by Pietro Basso on 29/06/2018.
// Copyright (c) 2018 Pietro Basso. All rights reserved.
//
let urls = ["http://youtu.be/NLqAF9hrVbY",
"http://www.youtube.com/watch?feature=player_embedded&v=DJjDrajmbIQ",
"http://www.youtube.com/watch?v=dQw4w9WgXcQ",
"http://www.youtube.com/embed/NLqAF9hrVbY",
@pietrobasso
pietrobasso / response.json
Created January 11, 2022 12:40
List of non-homogeneous JSON objects
{
"items": [
{
"type": "HeaderWidget",
"title": "Hello world",
"subtitle": "Lorem ipsum"
},
{
"type": "CardWidget",
"title": "Your top visits",
@pietrobasso
pietrobasso / Throwable.swift
Last active January 11, 2022 12:53
Throwable
struct Throwable<T: Decodable>: Decodable {
private let result: Result<T, Error>
init(from decoder: Decoder) throws {
do {
result = .success(try T(from: decoder))
} catch let error {
// additionally, logError(error)
result = .failure(error)
}
@pietrobasso
pietrobasso / ClassicResponse.swift
Created January 11, 2022 13:07
Classic Response
struct Response: Decodable {
let items: [Widget]
}
@pietrobasso
pietrobasso / ThrowableResponse.swift
Last active January 11, 2022 13:10
Throwable Response
struct Response {
let items: [Widget]
}
extension Response: Decodable {
enum CodingKeys: CodingKey {
case items
}
init(from decoder: Decoder) throws {
@pietrobasso
pietrobasso / response_v2.json
Created January 11, 2022 13:23
Response v2
{
"items": [
{
"type": "HeaderWidget",
"title": "Hello world",
"subtitle": "Lorem ipsum"
},
{
"type": "ChartWidget",
"title": "Your expenses path through the month",
@pietrobasso
pietrobasso / Widget.swift
Last active January 17, 2022 14:49
Widget model
enum Widget {
case headerWidget(HeaderWidget)
case cardWidget(CardWidget)
case buttonWidget(ButtonWidget)
}
extension Widget: Decodable {
enum `Type`: String, Decodable {
case headerWidget = "HeaderWidget"
case cardWidget = "CardWidget"
@pietrobasso
pietrobasso / Throwable.playground
Last active January 17, 2022 14:49
Throwable example usage!
import Foundation
// MARK: - Throwable
struct Throwable<T: Decodable>: Decodable {
private let result: Result<T, Error>
init(from decoder: Decoder) throws {
do {
result = .success(try T(from: decoder))
} catch let error {