Skip to content

Instantly share code, notes, and snippets.

struct Lock {
private var unfairLock = os_unfair_lock_s()
mutating func lock(_ work: () -> Void) {
self.lock()
work()
self.unlock()
}
mutating func trylock(_ work: () -> Void) -> Bool {
@davbeck
davbeck / AssociatedProperty.swift
Created February 10, 2020 17:53
Swift friendly wrapper around associated objects.
import ObjectiveC
extension objc_AssociationPolicy {
public static let retain = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN
public static let retainNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
public static let copy = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY
public static let copyNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC
public static let assign = objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN
}
import Foundation
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error.
@propertyWrapper
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable {
var wrappedValue: Wrapped?
init() {
self.wrappedValue = nil
}
@davbeck
davbeck / IfElseTernary.swift
Created October 22, 2019 21:09
Use if/else logic instead of a ternary operator in Swift. Don't do this.
func _if<T>(_ test: Bool, action: () -> T) -> T? {
if test {
return action()
} else {
return nil
}
}
extension Optional {
func _else(action: () -> Wrapped) -> Wrapped {
extension Collection where Element: FloatingPoint {
func average() -> Element {
return self.reduce(Element.zero, +) / Element(self.count)
}
}
extension Collection where Element: BinaryInteger {
func average() -> Double {
return Double(self.reduce(0, +)) / Double(self.count)
}
@davbeck
davbeck / Example.swift
Created September 10, 2019 14:38
RateLimiter.swift
import PlaygroundSupport
let limiter = RateLimiter(timeframe: 5)
PlaygroundPage.current.needsIndefiniteExecution = true
for i in 0..<100 {
limiter.perform {
print("perform", i)
}
struct Parent: View {
@State var count: Int = 0
var body: some View {
return VStack {
ImageLoadingView(url: URL(string: "https://github.com/recurser/exif-orientation-examples/blob/9c4ccfaea6bfd434ac1c4bb0750ac6fc5848a5f4/Landscape_8.jpg?raw=true")!)
Text("counter: \(count)")
}
.onAppear {
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { (timer) in
self.count += 1
/// The result of a binary search.
struct BinarySearchResult<Index> {
/// The index for the searched key.
///
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true.
var index: Index
/// Whether the key was found in the collection.
///
/// When performing a binary search, even if the value is not found in the collection, an index is returned which can be used to insert a new item at the correct location.
// adapted from https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Search
/// The result of a binary search.
struct BinarySearchResult<Index> {
/// The index for the searched key.
///
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true.
var index: Index
/// Whether the key was found in the collection.
import Foundation
import SQLite3
public enum SQLiteError: Error, LocalizedError {
case sqlite(code: Int32, message: String?)
case invalidDatabase
case invalidStatement
case invalidUTF8String