Skip to content

Instantly share code, notes, and snippets.

View radianttap's full-sized avatar
🤔
· · ·

Aleksandar Vacić radianttap

🤔
· · ·
View GitHub Profile
@swhitty
swhitty / Mutex.swift
Last active January 20, 2025 18:49
Backports the Swift 6 type Mutex<Value> to all Apple platforms that support OSAllocatedUnfairLock
// Backports the Swift 6 type Mutex<Value> to Swift 5 and all Darwin platforms via OSAllocatedUnfairLock.
// Lightweight version of https://github.com/swhitty/swift-mutex
// Feel free to use any part of this gist.
// Note: ~Copyable are not supported
#if compiler(>=6)
@available(iOS, introduced: 16.0, deprecated: 18.0, message: "use Mutex from Synchronization module included with Swift 6")
@available(macOS, introduced: 13.0, deprecated: 15.0, message: "use Mutex from Synchronization module included with Swift 6")
public struct Mutex<Value>: @unchecked Sendable {
@Jeehut
Jeehut / .editorconfig
Last active December 6, 2024 16:38
This file should be added to every SwiftPM project to ensure Xcode 16+ has the same indentation settings for everyone.
# EditorConfig is awesome: https://editorconfig.org
root = true
[*]
indent_style = space
tab_width = 8
indent_size = 4
end_of_line = lf
@rnapier
rnapier / Mutex.swift
Last active November 17, 2024 15:16
Mutex backport
// Version of Swift 6 Mutex, with minimal API (just withLock), portable to at least iOS 15, probably much earlier.
// I cannot yet promise it's actually correct.
@frozen
public struct Mutex<Value> {
private let buffer: ManagedBuffer<Value, os_unfair_lock>
public init(_ initialValue: Value) {
buffer = ManagedBuffer<Value, os_unfair_lock>.create(minimumCapacity: 1) { _ in initialValue }
buffer.withUnsafeMutablePointerToElements { lockPtr in
@kylehughes
kylehughes / TornRectangle.swift
Last active May 7, 2024 07:17
A rectangle shape for SwiftUI that can render any edge like a torn piece of paper.
// Copyright 2021 Kyle Hughes
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
@nicklockwood
nicklockwood / CodableVersioning.swift
Last active January 29, 2024 11:31
Example demonstrating how to use versioning for Codable structs
// This gist demonstrates how you can implement versioning for a Codable struct to support loading
// old serialized data after changing the structure. Notable features of this solution:
//
// * No need to make new properties optional, or to perform post-processing on the struct after
// loading in ordeer to populate missing values
// * No need to change the call site - from the outside this struct behaves just the same
// as if we had implemented codable directly in the normal way.
// * Versioning can be applied individually to parents or leaves in a larger tree of
// structs without affecting the other elements
// * This approach will work even if the original struct was not designed with versioning in mind
@jverkoey
jverkoey / UIFont+CustomizedDynamicType.m
Created April 14, 2021 01:07
Dynamic Type system fonts with custom point sizes, weight, and italics
static const CGFloat kFontWeightEpsilon = FLT_EPSILON;
@implementation UIFont (CustomizedDynamicType)
+ (nonnull UIFont *)preferredFontWithDefaultSize:(CGFloat)size
textStyle:(nonnull UIFontTextStyle)textStyle {
return [self preferredFontWithDefaultSize:size
textStyle:textStyle
fontWeight:UIFontWeightRegular
italic:NO];
enum Status: Decodable {
case completed, inProgress
case other(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
switch value {
case "completed": self = .completed
@shaps80
shaps80 / Models.swift
Last active January 9, 2021 16:48
Swift type for representing a UserAgent (includes an implementation similar of Apple’s Version from SPM)
import UIKit
extension UIDevice {
/*
List can be updated here:
https://gist.github.com/adamawolf/3048717
*/
internal static var models: String = """
@Akhu
Akhu / .gitignore
Last active January 2, 2023 11:10
Xcode 12 + Swift UI Gitignore
# Created by https://www.toptal.com/developers/gitignore/api/swiftpackagemanager,swift,xcode,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=swiftpackagemanager,swift,xcode,macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
import SwiftUI
import PlaygroundSupport
// constants
let deviceWidth: CGFloat = 320
let deviceHeight: CGFloat = 568
let hasFaceID = true // false for TouchID
struct Device: View {
var body: some View {