Skip to content

Instantly share code, notes, and snippets.

@casperzandbergenyaacomm
casperzandbergenyaacomm / ArrowImageGenerator.swift
Last active November 13, 2018 11:15 — forked from ale84/ArrowImageGenerator.swift
Helper class to generate simple arrows images of desired size, colour and direction using UIKit api.
//
// ArrowImageGenerator.swift
//
// Created by Alessio Orlando on 07/10/15.
// Copyright © 2015 Alessio Orlando. All rights reserved.
//
import Foundation
import UIKit
@casperzandbergenyaacomm
casperzandbergenyaacomm / DateRounding.swift
Created October 30, 2018 10:54
Rounding of date and time
import Foundation
extension Date {
/// Returns date where **-component** is rounded to its closest
/// multiple of **-amount**. Warning: month and day start at 1
/// so round(to: 6, .month) will either return month 1 or 7!
func round(to amount: Int, _ component: Calendar.Component) -> Date {
let cal = Calendar.current
var value = cal.component(component, from: self)
@casperzandbergenyaacomm
casperzandbergenyaacomm / DictionaryKeyPath.swift
Last active June 14, 2021 11:16 — forked from dfrib/DictionaryKeyPath.swift
Swift: Reading and writing to (possible) nested dictionaries for a given key path, using a recursive approach
// Inspired by: https://gist.github.com/dfrib/d7419038f7e680d3f268750d63f0dfae
import Foundation
extension Dictionary {
subscript(keyPath string: String) -> Value? {
get {
return self[keyPath: Dictionary.keyPath(for: string)]
}
set {
self[keyPath: Dictionary.keyPath(for: string)] = newValue
@casperzandbergenyaacomm
casperzandbergenyaacomm / EmptyOrNil.swift
Last active November 12, 2021 08:10
Adding readability for an if statement I often use.
protocol EmptyOrNil {
var isEmpty: Bool { get }
}
extension Optional where Wrapped: EmptyOrNil {
var isEmptyOrNil: Bool {
return self?.isEmpty ?? true
}
}
@casperzandbergenyaacomm
casperzandbergenyaacomm / ime-test.swift
Last active May 11, 2018 10:18
Implicit Member Expression, Global Constants, and categories Swift
// I wanted to use IME with my global constants (https://medium.com/@maxcampolo/fun-with-global-constants-in-swift-3e012aaec0c7)
// but not lose the functionality of catogories in my constants
// Problem:
extension String {
static let string1 = "Test string."
}
let a: String = String.string1