Skip to content

Instantly share code, notes, and snippets.

View maximkrouk's full-sized avatar
🇺🇦

Maxim Krouk maximkrouk

🇺🇦
View GitHub Profile
// THIS IS A UNIVERSAL SWIFT FUNCTION BIULDER
// FOR ARRAYS OF ANY TYPE
// tested on Swift 5.3.1
@_functionBuilder
public enum ArrayBuilder<Element> {
public typealias Expression = Element
public typealias Component = [Element]
@dduan
dduan / cube.swift
Last active November 26, 2020 08:55
A rotating 3-D cube in terminal. Written in Swift
/// A rotating 3-D cube in terminal
/// Only works on macOS
/// Run `swift cube.swift` in a terminal application to run it.
/// For controlling the cube, see comments for `Key` in code.
import Darwin
enum RawModeError: Error {
case notATerminal
case failedToGetTerminalSetting
import Foundation
public extension Dictionary where Key == String, Value == Any {
fileprivate func _get<T>(path: [String]) -> T? {
var root = self
for idx in 0 ..< path.count - 1 {
guard let _root = root[path[idx]] as? [String: Any] else {
return nil
}
@PimCoumans
PimCoumans / CustomButton.swift
Last active July 8, 2022 09:16
UIButton subclass with per-state custom values like background and image colors, extendable with whatever value you want to update
class Button: UIButton {
private class Subview: UIView {
// Never allow isUserInteractionEnabled in any button subview
override var isUserInteractionEnabled: Bool {
get { false }
set { super.isUserInteractionEnabled = false }
}
}
@IanKeen
IanKeen / LosslessCodable.swift
Last active May 22, 2024 20:06
PropertyWrapper: LosslessCodable attempts to brute force convert the incoming value to the required type - the underlying type is maintained and used when encoding the value back
public typealias LosslessStringCodable = LosslessStringConvertible & Codable
@propertyWrapper
public struct LosslessCodable<Value: LosslessStringCodable>: Codable {
private let type: LosslessStringCodable.Type
public var wrappedValue: Value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
@IanKeen
IanKeen / EntryPoint.swift
Last active September 8, 2024 05:37
Example main.swift
import Foundation
import SwiftUI
let isUITesting = /* your UI test detection here */
@main
struct EntryPoint {
static func main() {
if isUITesting {
UITestApp.main()
@MihaelIsaev
MihaelIsaev / Postgres+Transaction.swift
Last active January 16, 2020 01:34
Postgres transaction extension for Vapor4 and Fluent4
//
// Postgres+Transaction.swift
//
// Created by Mihael Isaev on 14.01.2020.
//
import Vapor
import FluentKit
import PostgresKit
@RuiAAPeres
RuiAAPeres / SwiftUIBindsWithReactiveSwift.swift
Last active August 5, 2024 07:54
Couple of methods to bridge ReactiveSwift with SwiftUI
import Combine
import ReactiveSwift
import SwiftUI
class AnySubscription: Subscription {
private let cancelable: Cancellable
init(cancelable: Cancellable) {
self.cancelable = cancelable
@4np
4np / HowTo use xcconfig or plist with SPM.md
Last active August 5, 2024 11:06
How to use a .xcconfig file and a .plist with a Swift Package Manager based project.

How to use a .xcconfig file and a .plist file with SPM

Worth a read for some more context.

Create a Package.xcconfig file

Create the file in the root of the project (where your Package.swift file lives as well), and use the following contents:

/// Package.xcconfig
@shaps80
shaps80 / CGPoint+Target.swift
Last active March 21, 2024 18:44
Distance travelled after decelerating to zero velocity at a constant rate. The included Playground file shows how you can use it with a pan gesture as an example.
public extension CGPoint {
// The target points after decelerating to 0 velocity at a constant rate
func target(initialVelocity: CGPoint, decelerationRate: CGFloat = UIScrollView.DecelerationRate.normal.rawValue) -> CGPoint {
let x = self.x + self.x.target(initialVelocity: initialVelocity.x, decelerationRate: decelerationRate)
let y = self.y + self.y.target(initialVelocity: initialVelocity.y, decelerationRate: decelerationRate)
return CGPoint(x: x, y: y)
}
}