Skip to content

Instantly share code, notes, and snippets.

@lattner
lattner / TaskConcurrencyManifesto.md
Last active May 3, 2024 08:18
Swift Concurrency Manifesto
@andymatuschak
andymatuschak / States-v3.md
Last active May 1, 2024 12:32
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active March 10, 2024 19:23
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>
@timonus
timonus / programmatic-dynamic-images.m
Last active January 1, 2024 12:08
Programmatically create iOS 13 dynamic images
- (UIImage *)dynamicImage
{
UITraitCollection *const baseTraitCollection = /* an existing trait collection */;
UITraitCollection *const lightTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]];
UITraitCollection *const purelyDarkTraitCollection = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
UITraitCollection *const darkTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, purelyDarkTraitCollection]];
__block UIImage *lightImage;
[lightTraitCollection performAsCurrentTraitCollection:^{
lightImage = /* draw image */;
@AdamLantz
AdamLantz / UIImage+Cropping.swift
Created June 20, 2018 16:03
Swift 4 - Crop transparent pixels from UIImage
//Swift 4 modifications for this gist: https://gist.github.com/krooked/9c4c81557fc85bc61e51c0b4f3301e6e
import Foundation
import UIKit
extension UIImage {
func cropImageByAlpha() -> UIImage {
let cgImage = self.cgImage
let context = createARGBBitmapContextFromImage(inImage: cgImage!)
let height = cgImage!.height
let width = cgImage!.width
@alessaba
alessaba / PlaygroundsFrameworks.swift
Last active July 29, 2023 21:47
List of available frameworks in Swift Playgrounds over the years
// Swift Playgrounds Beta 1.0
import AVFoundation
import AVKit
import Accelerate
import Accounts
import AudioToolbox
import AudioUnit
import CFNetwork
import CoreAudio
import CoreAudioKit
@airspeedswift
airspeedswift / OneSidedRanges.swift
Last active April 21, 2023 17:14
One-sided Range operators
postfix operator ..< { }
prefix operator ..< { }
struct RangeStart<I: ForwardIndexType> { let start: I }
struct RangeEnd<I: ForwardIndexType> { let end: I }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I>
{ return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I>
@fastred
fastred / NS_SWIFT_UNAVAILABLE.md
Last active February 6, 2022 15:35
NS_SWIFT_UNAVAILABLE

Results of searching for NS_SWIFT_UNAVAILABLE macro in iOS 9 and OS X 10.11 headers reveal some new Swift-specific APIs.

Highlights

GameplayKit.framework/Headers/GKEntity.h
70: - (nullable GKComponent *)componentForClass:(Class)componentClass
NS_SWIFT_UNAVAILABLE("Exposed in Swift as componentForClass<ComponentType: GKComponent>(componentClass: ComponentType.Type) -> ComponentType?");

GameplayKit.framework/Headers/GKStateMachine.h
@zats
zats / script.swift
Last active March 5, 2021 01:32
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
@dennisvennink
dennisvennink / headAndTail.swift
Last active January 17, 2021 09:49 — forked from ole/headAndTail.swift
Swift challenge: Sequence.headAndTail
extension Sequence {
var headAndTail: (head: Element, tail: SubSequence)? {
var head: Element?
let tail = drop {
if head == nil {
head = $0
return true
} else {