Skip to content

Instantly share code, notes, and snippets.

View phlippieb's full-sized avatar
🦆

Phlippie phlippieb

🦆
View GitHub Profile
@phlippieb
phlippieb / ExactNumberOfIndices.swift
Created July 7, 2022 05:00
Leveraging Swift's type system to represent the concept of an index type with a fixed size
/**
Leveraging Swift's type system to represent the concept of an index type with a fixed size,
so that iterating over all indices doesn't require a default case.
*/
/// The abstract properties of a fixed-size index type
protocol ExactNumberOfIndices: CaseIterable, RawRepresentable where RawValue == Int {}
/// A concrete fixed-size index type with 1 element (namely 0)
enum _1: Int, ExactNumberOfIndices {
@phlippieb
phlippieb / Swift-Tagged.md
Created December 2, 2021 11:52
A pattern for preventing intermixed types that represent non-interchangeable concepts

The problem

Consider two models defined like this:

struct User {
  let id: Int
  let name: String
  let email: String
}
@phlippieb
phlippieb / Sign in with Apple.swift
Last active September 30, 2019 06:25
Extremely simple sign in with apple test app
/*
In anticipation of iOS 13, I'm implementing Sign in with Apple.
To get started, I just made a simple VC that brings up the auth stuff, without even handling the result.
When running it in a simulator and completing the sign in process, the process appears to be broken:
- The auth controller is not dismissed
- `ASAuthorizationController`'s delegate method `authorizationController:didCompleteWithAuthorization` is not called
The code below is intended as a simple copy+paste to make problem replication easier:
1. Create a new XCode project
/*
Given an array with optional sub-arrays, how can I flatten the array?
Example:
Given [[1, 2], nil, [3]]
Expect [1, 2, 3]
Solution:
// Say you have a generic class with a couple of generic types,
// that also takes a couple of parameters in its initializer:
class Foo<T, U, V> {
init(label: String, other: Any) {
// ...
}
}
// Instantiating objects of this class in the standard way, while not unusual,
//
// TableviewDatasource.swift
//
// Created by Phlippie Bosman on 2019/01/30.
// Copyright © 2019 Kalido. All rights reserved.
//
import Reusable
import SwiftLCS
/// Synched operations will only run one at a time.
/// The `operation` method is responsible for calling `complete` to notify the object when it is done.
final class SyncedOp {
// Use a GCD Semaphore to synchronize flag access.
private let semaphore = DispatchSemaphore(value: 1)
private var isBusy = false
private var isRetriggerQueued = false
internal var operation: () -> () = {}
@phlippieb
phlippieb / ResizingTableViewCellDelegate.swift
Created March 15, 2019 06:57
Even when using AutoLayout and UITableView.automaticDimension, it's not obvious how to make your table view resize a cell when its content changes without reloading the entire cell. I've had success with calling beginUpdates() and endUpdates() on the table view after the content changes. This gist simply extracts that into a delegate.
/*
This code demonstrates a way to get UITableViewCells to resize
after their content size has changed, without needing to reload
the cells.
Prerequisites:
- The table view must be set up to calculate its cell heights dynamically; i.e.
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100 // or some good estimate
- The cell must be laid out using auto layout. Remember to add the subviews to
@phlippieb
phlippieb / LightRibbonView.swift
Last active April 16, 2018 07:15
A UIView subclass that draws an animated light ribbon.
//
// LightRibbon2.swift
// Waveforms
//
// Created by Phlippie Bosman on 2018/04/12.
// Copyright © 2018 Phlippie Bosman. All rights reserved.
//
import UIKit
# To get this:
# curl -Lo $HOME/.screenrc 'https://goo.gl/bZv3x2'
# Enable mouse scrolling:
termcapinfo xterm* ti@:te@
# Clear screen when vim/less/etc quits:
altscreen on
# Status line that shows windows: