Skip to content

Instantly share code, notes, and snippets.

View T1T4N's full-sized avatar
👽

Robert Armenski T1T4N

👽
View GitHub Profile
@T1T4N
T1T4N / vscode-local-workspace.json
Created June 22, 2024 21:45
An example VSCode configuration for color-coding workspace windows
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#dc0000",
"activityBar.activeBorder": "#03A9F4",
"activityBar.background": "#860009",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#03A9F4",
"activityBarBadge.foreground": "#e7e7e7",
"statusBar.background": "#03A9F4",
@T1T4N
T1T4N / Publisher+WeakAssign.swift
Created June 22, 2024 10:14
Combine extensions for weak assignment and sink
extension Publisher where Failure == Never {
func weakAssign<T: AnyObject>(
to keyPath: ReferenceWritableKeyPath<T, Output>, on object: T
) -> AnyCancellable {
sink { [weak object] value in
object?[keyPath: keyPath] = value
}
}
func weakSink<T: AnyObject>(
@T1T4N
T1T4N / Publisher+Async.swift
Last active June 21, 2024 12:17
A Swift Extension for converting a Combine Publisher to async/await
extension Publisher {
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func asyncFirst() async throws -> Output? {
return try await self
.first()
.values
.first { _ in true }
}
// https://medium.com/geekculture/from-combine-to-async-await-c08bf1d15b77
@T1T4N
T1T4N / Memoize.swift
Created June 21, 2024 07:48
Swift memoization helpers
// Source: https://www.hackingwithswift.com/plus/high-performance-apps/using-memoization-to-speed-up-slow-functions
// Source: https://medium.com/@mvxlr/swift-memoize-walk-through-c5224a558194
typealias MemFn<Input, Output> = (Input) -> Output
// work with any sort of input and output as long as the input is hashable
// accept a function that takes Input and returns Output, and return a function with the same signature
func memoize<Input: Hashable, Output>(
_ work: @escaping MemFn<Input, Output>
) -> MemFn<Input, Output> {
@T1T4N
T1T4N / gist:f66a2ebbeb17b73d6643a178ede14639
Created June 18, 2024 14:54
Disable Xcode 16's Predictive Code Completion LLM Model
defaults write com.apple.dt.Xcode IDEModelAccessHasUserConsentForOnDeviceInteractions -bool FALSE
@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 / 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 / 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