Skip to content

Instantly share code, notes, and snippets.

View isaac-weisberg's full-sized avatar
💭
🤔🤔🤔

Isaac Weisberg isaac-weisberg

💭
🤔🤔🤔
View GitHub Profile
@nalexn
nalexn / CancelBag.swift
Last active November 1, 2023 06:43
Collecting AnyCancellable tokens in declarative SwiftUI fashion
// Copyright © 2019 Alexey Naumov. MIT License
import Combine
typealias CancelBag = Set<AnyCancellable>
extension CancelBag {
mutating func collect(@Builder _ cancellables: () -> [AnyCancellable]) {
formUnion(cancellables())
}
@Winchariot
Winchariot / KeyboardAccessoryView.swift
Last active November 22, 2023 14:26
UIToolbar with a right-aligned "Done" button, for use as a keyboard accessory view
lazy var accessoryToolbarWithDoneButton: UIToolbar = {
let toolbar = UIToolbar(frame: CGRect.zero)
//only needs to be flexible in the dimension(s) you want it to be
//https://stackoverflow.com/questions/31822504/how-can-i-increase-the-height-of-an-inputaccessoryview
toolbar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let leftSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(keyboardDoneButtonTapped(_:)))
toolbar.items = [leftSpace, doneButton]
struct FailableDecodable<Base: Decodable>: Decodable {
var base: Base?
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
base = try container.decode(Base.self)
} catch let error {
print(error)
}
@krin-san
krin-san / CFNetworkErrors.h
Last active September 28, 2023 10:26
Apple NSURLError and CFNetworkError codes
typedef CF_ENUM(int, CFNetworkErrors) {
kCFHostErrorHostNotFound = 1,
kCFHostErrorUnknown = 2, // Query the kCFGetAddrInfoFailureKey to get the value returned from getaddrinfo; lookup in netdb.h
// SOCKS errors; in all cases you may query kCFSOCKSStatusCodeKey to recover the status code returned by the server
kCFSOCKSErrorUnknownClientVersion = 100,
kCFSOCKSErrorUnsupportedServerVersion = 101, // Query the kCFSOCKSVersionKey to find the version requested by the server
// SOCKS4-specific errors
kCFSOCKS4ErrorRequestFailed = 110, // request rejected or failed by the server
kCFSOCKS4ErrorIdentdFailed = 111, // request rejected because SOCKS server cannot connect to identd on the client
@vukcevich
vukcevich / Anonymous-Class-In-Swift.playground
Created October 10, 2017 15:51
Anonymous class in Swift - equivalent techniques
//: Playground - noun: a place where people can play
import UIKit
var str = "Anonymous Class in Swift"
//https://stackoverflow.com/questions/24408068/anonymous-class-in-swift
//There is no equivalent syntax.
//Regarding equivalent techniques, theoretically you could use closures and define structs and classes inside them.
import Foundation
import SceneKit
let json = """
{
"position": [2.0, 5.0, 3.0],
"rotation": [0.0, 0.73, 0.0, 0.73]
}
""".data(using: .utf8)!
@lattner
lattner / TaskConcurrencyManifesto.md
Last active June 1, 2024 06:16
Swift Concurrency Manifesto
@mcmurrym
mcmurrym / MirrorDebugDecscription.swift
Last active December 24, 2019 05:59
A default protocol implementation for CustomDebugStringConvertible that uses Mirror for introspection
public extension CustomDebugStringConvertible {
var debugDescription: String {
return debugDescription()
}
func debugDescription(_ indentationLevel: Int = 0, includeType: Bool = true) -> String {
let indentString = (0..<indentationLevel).reduce("") { tabs, _ in tabs + "\t" }
var s: String
@remojansen
remojansen / class_decorator.ts
Last active September 14, 2023 14:54
TypeScript Decorators Examples
function logClass(target: any) {
// save a reference to the original constructor
var original = target;
// a utility function to generate instances of a class
function construct(constructor, args) {
var c : any = function () {
return constructor.apply(this, args);
}
@Ben-G
Ben-G / DynamicInit.swift
Last active May 27, 2023 13:30
Dynamically create instance based on type in Swift
protocol Initializable {
init()
}
class A : Initializable {
var content:String
required init() {
content = "TestContent"
}