Skip to content

Instantly share code, notes, and snippets.

View T1T4N's full-sized avatar
👽

Robert Armenski T1T4N

👽
View GitHub Profile
@T1T4N
T1T4N / gist:1038bc05d602cec48ff5802709aad7d1
Created April 15, 2024 13:47
Swift flags to disable automatic linking
// https://milen.me/writings/auto-linking-on-ios-and-macos/
// This works
OTHER_SWIFT_FLAGS = $(inherited) -Xfrontend -disable-autolink-framework -Xfrontend TestFramework
// This doesn't work, even though it is referenced here:
// https://github.com/apple/swift/blob/main/include/swift/Option/FrontendOptions.td#L531C47-L531C74
//OTHER_SWIFT_FLAGS = $(inherited) -Xfrontend -disable-autolink-frameworks
@objc public protocol TestProto1 {}
public protocol TestProto2: Hashable {}
public enum Destination: Hashable, Equatable {
// OKAY - Concrete
case test(obj: NSObject)
// Error: any type (not concrete/boxed)
// Type 'AppFeatureDestination' does not conform to protocol 'Equatable'
case test1(obj: NSObject & TestProto1)
@T1T4N
T1T4N / MyAnyHashable.swift
Created April 12, 2024 10:18
A custom simple AnyHashable implementation
protocol HashableBox: Hashable {
func `as`<T : Hashable>(_: T.Type) -> T?
func isEqual(_ other: any HashableBox) -> Bool
}
struct ConcreteHashableBox<Base: Hashable>: HashableBox {
let base: Base
func `as`<T>(_: T.Type) -> T? where T : Hashable {
return base as? T
@T1T4N
T1T4N / generate-xcode-compilation-database.md
Last active April 2, 2024 07:29
Generate a JSON Compilation Database from an Xcode project

Introduction

A JSON compilation database is a very handy output format which is parsed and used by many development tools. Unfortunately for us Apple Developers, it is not straightforward to generate one from within Xcode, as it is (probably) not Apple's priority and therefore there is no toggle/switch/setting that can be easily enabled to get this information.

There is however a solution, thanks to Apple using Clang/LLVM as their main toolchain.

Implementation

The standard way to generate this with clang would be to use the -MJ flag and give it a file name that typically corresponds to the input file. Using this flag indirectly through Xcode is hard, given that we're not aware of all the other arguments when a compiler call is executed.

However, there is a second hidden/badly documented LLVM flag: -gen-cdb-fragment-path - it is implemented in terms of -MJ and has the same functionality, but it's argument in contrast is an output directory.

@T1T4N
T1T4N / LeakMemory.swift
Last active February 16, 2024 10:13
A snippet to leak as much of the available virtual memory in Swift as possible
// Modified from: https://stackoverflow.com/a/45882589
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
var freeMem = Int.max
let reserved = 605696
let chunks = 12000
repeat {
freeMem = os_proc_available_memory()
let toAlloc = freeMem
let mallocSize = toAlloc / chunks
@T1T4N
T1T4N / update-a-git-mirror.sh
Last active February 8, 2024 09:37
Mirroring a git repo
git clone --bare https://github.com/project/repo.git
cd repo
git push --mirror ssh://git@bitbucket/lib/new-repo.git
#f this results in the following error, go to step 4: 
#You are attempting to update refs that are reserved for Bitbucket's pull request functionality. Bitbucket manages these refs automatically, and they may not be updated by users.
#remote: Rejected refs:
#remote:     refs/pull-requests/1/from
git push ssh://git@bitbucket/lib/new-repo.git
@T1T4N
T1T4N / mirror-git-repo.sh
Last active February 7, 2024 10:37
Mirroring a git repository to Bitbucket
git clone --bare https://github.com/project/repo.git
cd repo
git push --mirror ssh://git@bitbucket/lib/new-repo.git
# if this results in the following error:
# You are attempting to update refs that are reserved for Bitbucket's pull request functionality. Bitbucket manages these refs automatically, and they may not be updated by users.
# remote: Rejected refs:
# remote: refs/pull-requests/1/from
#
# then use the following commands
@T1T4N
T1T4N / dynamic_dependencies.sh
Created January 29, 2024 08:42
List all dynamically linked framework dependencies
# Get all dynamically linked framework dependencies
# sort -u is necessary for FAT targets such as iphonesimulator builds
otool -L "/path/to/Example.framework/Example" | \
grep '@rpath' | grep '.framework' | awk '{ print $1 }' | cut -d '/' -f2 | \
xargs -I{} basename "{}" ".framework" | \
sort -u | \
grep -v "Example" | \
while IFS=$'\n' read -r dependency; do
echo "$dependency"
done
@T1T4N
T1T4N / CVPixelBuffer+Data.swift
Last active November 7, 2023 15:29
A format-agnostic way of converting CVPixelBuffer to Data and back
extension CVPixelBuffer {
public static func from(_ data: Data, width: Int, height: Int, pixelFormat: OSType) -> CVPixelBuffer {
data.withUnsafeBytes { buffer in
var pixelBuffer: CVPixelBuffer!
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, pixelFormat, nil, &pixelBuffer)
guard result == kCVReturnSuccess else { fatalError() }
CVPixelBufferLockBaseAddress(pixelBuffer, [])
defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, []) }
@T1T4N
T1T4N / URLSession+DataTaskProgressPublisher.swift
Last active May 2, 2023 07:39
Emulating a DataTaskPublisher with Progress
#if canImport(Combine)
import Foundation
import Combine
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension URLSession {
public typealias DataTaskProgressPublisher =
(progress: Progress, publisher: AnyPublisher<DataTaskPublisher.Output, Error>)