Skip to content

Instantly share code, notes, and snippets.

View BasThomas's full-sized avatar

Bas Broek BasThomas

View GitHub Profile
import UIKit
protocol Component: NSObjectProtocol {
var accessibilityTraits: UIAccessibilityTraits { get set }
}
public final class SpecialComponent: UIView, Component {}
let mySpecialComponent: Component = SpecialComponent(frame: .zero)
mySpecialComponent.accessibilityTraits = .staticText
@BasThomas
BasThomas / xcode.md
Created September 1, 2018 08:06
Terminal commands
  • defaults write com.apple.dt.Xcode IDEIndexerActivityShowNumericProgress -bool true
struct A: Hashable { }
struct B: Hashable { // error: type 'B' does not conform to protocol 'Hashable'
let aa: [A]
}
@BasThomas
BasThomas / shim-swift-4-1.swift
Last active April 11, 2018 21:53
Backward compatibility for `compactMap` in Swift 4.0.
#if swift(>=4.1)
#else
extension Collection {
func compactMap<ElementOfResult>(
_ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult] {
return try flatMap(transform)
}
}
#endif
@BasThomas
BasThomas / moving-average.swift
Last active April 4, 2023 10:16
Calculate a moving average in Swift (Swift 4.1)
extension Collection where Element == Int, Index == Int {
/// Calculates a moving average.
/// - Parameter period: the period to calculate averages for.
/// - Warning: the supplied `period` must be larger than 1.
/// - Warning: the supplied `period` should not exceed the collection's `count`.
/// - Returns: a dictionary of indexes and averages.
func movingAverage(period: Int) -> [Int: Float] {
precondition(period > 1)
precondition(count > period)
@BasThomas
BasThomas / Sequence+Contains.swift
Created March 6, 2018 18:29
A `containsOnly` and `containsNone` implementation for `Sequence`.
import Foundation
extension Sequence where Element: Equatable {
/// Returns a Boolean value indicating whether every element of the sequence
/// is equal to the given element.
func containsOnly(_ element: Element) -> Bool {
var iterator = self.makeIterator()
guard iterator.next() != nil else { return false }
return first(where: { $0 != element }) == nil
import Foundation
struct Person: Codable {
public let name: String
}
let person = Person(name: "Bas")
let payload = try JSONEncoder().encode(person)
let string = String(data: payload, encoding: .utf8) // {"name":"Bas"}
let decodedPerson = try JSONDecoder().decode(Person.self, from: payload) // Person(name: "Bas")
import UIKit
class VC: UIViewController {
func formatter() -> DateFormatter {
struct Formatter {
static let f: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium
print("I am called just once")
return f
@BasThomas
BasThomas / matrix.swift
Last active November 24, 2017 14:54
Switching on optional types
let o1nil: Int? = nil
let o1: Int? = 1
func switcher(_ a: Int?, _ b: Int?) {
switch (a, b) {
case (nil, nil):
print("nothing")
case (let thing?, nil):
print("lhs", thing)
case (nil, let thing?):
import Foundation
URL(string: "https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch_reading_ease") // nil
URL(string: "https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch_reading_ease") // valid
URL(string: "https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch.E2.80.93Kincaid_grade_level") // nil
URL(string: "https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch.E2.80.93Kincaid_grade_level") // valid
"https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch_reading_ease" ==
"https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch_reading_ease" // false