Skip to content

Instantly share code, notes, and snippets.

View BasThomas's full-sized avatar

Bas Broek BasThomas

View GitHub Profile
/*
Returns the localized label(s) that should be provided by the user to refer to this element.
Use this property when the accessibilityLabel is not appropriate for dictated or typed input.
For example, an element that contains additional descriptive information in its accessibilityLabel can return a more concise label.
The primary label should be first in the array, optionally followed by alternative labels in descending order of importance.
If this property returns an empty or invalid value, the accessibilityLabel will be used instead.
default == an empty array
default on UIKit controls == an array with an appropriate label, if different from accessibilityLabel
*/
@available(iOS 13.0, *)
protocol MyProtocol {
func doThe(thing: String)
}
extension MyProtocol {
func doThe(thing: String = "") {
print("protocol")
}
}
struct Let {
let x: String?
}
struct Var {
var x: String?
}
let l1 = Let(x: nil)
// let l2 = Let() // not available
let registration = UICollectionView.CellRegistration<CardCollectionViewCell, Card> { cell, indexPath, card in
cell.card = card
}
let dataSource = UICollectionViewDiffableDataSource<Column, Card>(
collectionView: collectionView
) { collectionView, indexPath, card -> UICollectionViewCell in
collectionView.dequeueConfiguredReusableCell(
using: registration,
for: indexPath,
item: card
import Foundation
import XCTest
struct WompWomp: Error {
}
func myThrowingFunction() throws -> Int {
throw WompWomp()
}
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
extension Locale {
var defaultTemperatureUnit: UnitTemperature {
if #available(iOS 16, *) {
return UnitTemperature(forLocale: self, usage: .general)
} else {
let mCel = Measurement(value: 0, unit: UnitTemperature.celsius)
let f = MeasurementFormatter()
f.locale = self
let s1 = f.string(from: mCel)
@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)