Skip to content

Instantly share code, notes, and snippets.

NSInternalInconsistencyException: Call must be made on main thread
0 CoreFoundation 0x196c40cb4 __exceptionPreprocess + 164 (NSException.m:202)
1 libobjc.A.dylib 0x18fcd83d0 objc_exception_throw + 60 (objc-exception.mm:356)
2 Foundation 0x1913ce54c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 188 (NSException.m:242)
3 UIKitCore 0x1999f3da0 -[UIApplication _performBlockAfterCATransactionCommitSynchronizes:] + 404 (UIApplication.m:3377)
4 UIKitCore 0x199a00318 -[UIApplication _updateStateRestorationArchiveForBackgroundEvent:saveState:exitIfCouldNotRestoreState:updateSnapshot:windowScene:] + 528 (UIApplication.m:11822)
5 UIKitCore 0x199a005dc -[UIApplication _updateSnapshotAndStateRestorationWithAction:windowScene:] + 144 (UIApplication.m:11867)
6 <REDACTED> 0x104bbbc04 @objc closure #1 in <REDACTED>.userNotificationCenter(_:
@jpsim
jpsim / FB13265427.md
Created October 12, 2023 21:00
[FB13265427] ScrollView + LazyVStack is jittery starting with iOS 17

When a LazyVStack is nested within a ScrollView on iOS 17, the scroll view's content offset can be incorrectly set for a brief moment, causing the scroll view to "jitter".

Here's some sample code:

import SwiftUI

struct ContentView: View {
    @State var isExpanded = Set<Int>()
    var body: some View {
@jpsim
jpsim / 1 - SwiftLint Bloaty.md
Last active December 7, 2022 19:58
SwiftLint Bloaty

This gist documents the steps to analyze the binary size allocation for SwiftLint (and other SwiftPM-based executables).

First, install Bloaty McBloatface: https://github.com/google/bloaty

Then run the following:

$ swift build -c release
$ ditto .build/release/swiftlint swiftlint-stripped
$ strip -rSTX swiftlint-stripped
@jpsim
jpsim / scan-shc.swift
Last active January 31, 2023 22:25
Swift script to scan SHC QR images
import AppKit
import Compression
import CoreImage
import Foundation
import Vision
// MARK: - Get QR code image from specified path
guard CommandLine.arguments.count == 2 else {
print("Usage: scan-shc [path-to-qr-image]")
@jpsim
jpsim / parallel_map.swift
Created June 11, 2021 15:18
Parallel map in Swift
extension Array {
func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer.map { $0! }
}
}
@jpsim
jpsim / FB7736428.md
Created June 14, 2020 04:48
FB7736428: SwiftUI navigation bar items unresponsive

With the following code:

import SwiftUI

struct ContentView: View {
    @State private var showPopover = false

    var body: some View {
 NavigationView {
@jpsim
jpsim / FB6395442.md
Created July 24, 2019 03:24
FB6395442: Running 'xcodebuild archive' on a SwiftPM product produces different results than an equivalent .xcodeproj

An Xcode project generates the following archive:

$ tree Yams-macosx.xcarchive
Yams-macosx.xcarchive
├── Info.plist
├── Products
│   └── Library
│       └── Frameworks
│           └── Yams.framework
@jpsim
jpsim / LineEndsFind.mm
Created April 22, 2018 13:20 — forked from iosdevzone/LineEndsFind.mm
A function to find the offsets of newlines ('\n') in UTF-16 encoded string. Try as I might, I cannot get a Swift version within an order of magnitude of the C++ version. Both routines must return arrays of the same size and with equal elements.
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <iostream>
#import <vector> // Needed for gist to compile.
#pragma mark - Pure Implementation Functions
const static unichar kUTF16Newline = (unichar)'\n'; // old naming habits die hard!
/**
* Calculates an array of line end "positions" for a given string.
* The equivalent Swift function was `(String) -> [Int]` or `(NSString) -> [Int]`
*
@jpsim
jpsim / jazzy_linux.md
Last active July 3, 2020 20:26
Jazzy Linux

Running jazzy from a fresh Ubuntu machine:

Create a brand new Ubuntu machine

$ doctl compute droplet create jazzy --size 16gb \
    --image ubuntu-16-10-x64 --region sfo1
$ doctl compute ssh jazzy

Install Swift

@jpsim
jpsim / swift_dev_linux.md
Created December 14, 2017 18:18
Build & run a subset of Swift tests on Linux

Build & run a subset of Swift tests on Linux

# Spin up a beefy Digital Ocean droplet
doctl compute droplet create swift-dev --size m-224gb --image ubuntu-16-10-x64 --region sfo2 --ssh-keys XXXXXXX
doctl compute ssh swift-dev
# Install dependencies
apt-get update
apt-get install git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev autoconf libtool systemtap-sdt-dev tzdata
# Clone &amp; build Swift