Skip to content

Instantly share code, notes, and snippets.

@manmal
manmal / AsyncObservableObject.swift
Last active June 11, 2021 15:32
Async ObservableObject
import SwiftUI
@MainActor
class PhotoStore: ObservableObject {
@Published private(set) var isSaving: Bool = false
// Made nonisolated because Playgrounds does not run on
// @MainActor. There's probably a cleaner way of doing this
nonisolated init() {}
@manmal
manmal / ActorReentrancyProblem.swift
Last active June 8, 2021 18:44
Potential reentrancy problem in Swift Actors
import Foundation
// Crashes when count is set to 683. Works fine if count < 683.
// Usage:
// func testMessagePassing() throws {
// let coordinator = Coordinator()
// let expectation = XCTestExpectation()
// detach {
// await coordinator.simulateMessengers()
@manmal
manmal / UIImage+LaunchImage.swift
Created May 12, 2021 08:07
UIImage+LaunchImage
import UIKit
extension UIImage {
/// Available launch images (as of 12 May 2021):
/// [email protected]: 750x1334
/// [email protected]: 828x1792
/// [email protected]: 1242x2208
/// [email protected]: 1125x2436
/// [email protected]: 1242x2688
static var launchImage: UIImage {
@manmal
manmal / CombineHelper.swift
Created March 30, 2021 08:09 — forked from steipete/CombineHelper.swift
Combine helper that runs a Future after a delay on a background queue
import Foundation
import Combine
func runDelayedInBackground<Output, DelayingScheduler: Scheduler, ReceivingScheduler: Scheduler>(
delay: Int = 200,
scheduler: DelayingScheduler,
receiveOn: ReceivingScheduler,
worker: @escaping () -> Result<Output, Never>,
completion: @escaping (Output) -> ()
) -> AnyCancellable {
@manmal
manmal / SharedState.swift
Last active April 25, 2021 08:18
Shared State for The Composable Architecture - Make state accessible to sub components
// Copyright (c) 2021 Manuel Maly
// 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
@manmal
manmal / SelfRetainedByEmptyMethodCapture.swift
Created December 24, 2020 09:12
Swift object and struct instances are retained if an empty method of theirs is captured
import Foundation
final class A {
func someMethod() {}
}
struct B {
let a: A
func someMethod() {}
}
import Foundation
import PlaygroundSupport
import UIKit
enum PassingStructInInit {
struct A {
let b: B
}
@manmal
manmal / DeferredJust.swift
Last active November 23, 2021 19:51
Combine Publisher sending one value on each subscription. Unlike Just, the value is determined at subscription time.
// MIT License
//
// Copyright (c) 2020 Manuel Maly
// 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:
//
@manmal
manmal / remove_proxies_unless_charles_runs.sh
Last active October 24, 2020 11:08
Remove macOS proxies set by Charles, unless Charles is running
# Whenever Charles is quit unexpectedly (e.g. hard reboot due to kernel panic),
# it leaves its proxy settings active, breaking outbound internet connections.
# This has happened so often to me recently that I decided I need a script for this.
#
# Store this in an .sh file, and make it executable with
# chmod x+ {PATH_TO_FILE}
# I put it in my user's crontab to run every minute (even when logged out):
# echo "* * * * * /Users/{YOUR_USERNAME}/{...}/remove_proxies_unless_charles_runs.sh" | crontab -
# Since I'm impatient, I want this to run every 30 seconds even, so I also added:
# echo "* * * * * sleep 30; /Users/{YOUR_USERNAME}/{...}/remove_proxies_unless_charles_runs.sh" | crontab -
@manmal
manmal / print_advertisingid_usage.sh
Created September 14, 2020 08:51
Find IDFA usage in 3rd party frameworks
for framework in Frameworks/*.framework; do
fname=$(basename $framework .framework)
echo $fname
otool -v -s __TEXT __objc_methname $framework/$fname | grep advertisingIdentifier
done