Skip to content

Instantly share code, notes, and snippets.

View buh's full-sized avatar
📦
Swift

Alexey Bukhtin buh

📦
Swift
  • Amsterdam
View GitHub Profile
@chriszielinski
chriszielinski / Image+Trim.swift
Last active December 4, 2024 00:06
[Swift 5] NSImage/UIImage Crop/Trim Transparency
// Image+Trim.swift
//
// Copyright © 2020 Christopher Zielinski.
// https://gist.github.com/chriszielinski/aec9a2f2ba54745dc715dd55f5718177
//
// 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
@wtsnz
wtsnz / ContentView.swift
Last active February 10, 2025 09:46
Playing around with hosting SwiftUI Views in an NSWindow from inside a View 🙃 (also works for the NSStatusBar item!)
import SwiftUI
struct ContentView: View {
@State var now = Date()
@State var showWindow = false
@State var text: String = ""
let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
@loromits
loromits / ColorInterpolation.swift
Created September 4, 2019 09:15
Create gradient color interpolation of 2 UIColors
import UIKit
extension Comparable {
func clamping(_ range: ClosedRange<Self>) -> Self {
return Swift.min(range.upperBound, max(range.lowerBound, self))
}
}
extension UIColor {
func components() -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
@shaps80
shaps80 / 1. UserDefaults+Key.swift
Last active September 25, 2020 08:29
Adds a Swift extension to make UserDefaults more consistent to work with.
//
// UserDefaults.swift
//
// Created by Shaps Benkau on 24/05/2018.
// Copyright © 2018 152percent Ltd. All rights reserved.
//
import Foundation
#if os(iOS)
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active March 6, 2025 20:48
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@lsavino
lsavino / compilation-optimization.md
Last active July 27, 2022 17:44
Compiler Optimizations, Compiling Optimally, and Whole Modules

DEPRECATED for Xcode 10 🎈

(check out What's New in Swift at 11:40, slide 42)

Whole Module Compilation Optimizations: Why these terms are sometimes misleading

When you look up how to compile swift faster for debug builds, people very earnestly give advice that seems contradictory: you should "try using the whole module optimization flag," and also "never use whole module optimization for debugging". [^1]

This is confusing because some of us are using these two general words:

compilation: "turning text into an executable program"

@aman-tiwari
aman-tiwari / wfc-elabel-connected.lp
Last active May 29, 2020 17:35
asp map generation with connectivity constraints
#const n = 5.
#const m = 5.
% define?
cell(1..m, 1..n).
% tiles, format:
% pdef((tile name, rotation in 90deg increments), bottom edge, right edge, top edge, left edge)
pdef((line_v, 0), empty, path, empty, path).
pdef((line_v, 1), path, empty, path, empty).
@magicien
magicien / Swift4_JSON_09.swift
Created October 3, 2017 00:54
How should I inherit Codable class?
import Foundation
let json = """
{
"a": 1,
"b": 2,
"c": 3
}
""".data(using: .utf8)!
@samwize
samwize / DynamicKey.swift
Created September 28, 2017 06:59
A CodingKey that is dynamic -- it can be any string! Encode/decode with a Dictionary of `[String : Any]` in the model.
/**
```
// Encode a model with properties of type [String : Any]
var propertiesContainer = container.nestedContainer(keyedBy: DynamicKey.self, forKey: .properties)
if let properties = properties {
try propertiesContainer.encodeDynamicKeyValues(withDictionary: properties)
}
```
*/
struct DynamicKey: CodingKey {
@SheldonWangRJT
SheldonWangRJT / iOS Create a Carthage Supported Framework.txt
Last active August 28, 2023 09:28
iOS Create a Carthage Supported Framework
iOS Create a Carthage Supported Framework
#iOSBySheldon
A little bit Background of Carthage:
- Carthage works like Cocoapods, but a little bit simpler.
* When creating a Cocoapods supported framework need a .podspec file to do some config, which is not required by Carthage.
* When using a Cocoapods supported framework, a workspace file will be automatically created, the umbrella header will be setup in a pod project inside workspace. It feels simple, but the whole process creates a lot files including a "giant" workspace.
* When using a Carthage supported framework, it will just download the framework, and its dependencies if any, to a folder. All we need to do is to drag and drop it to the binary linking section in the project build setting tab, for the first time. It sounds complicated but it makes the project clean.
- Carthage is written in Swift.
- Carthage internally uses `-xcodebuild` to build dependency.