Skip to content

Instantly share code, notes, and snippets.

View phlippieb's full-sized avatar
🦆

Phlippie phlippieb

🦆
View GitHub Profile
//
// TableviewDatasource.swift
//
// Created by Phlippie Bosman on 2019/01/30.
// Copyright © 2019 Kalido. All rights reserved.
//
import Reusable
import SwiftLCS
// 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,
/*
Given an array with optional sub-arrays, how can I flatten the array?
Example:
Given [[1, 2], nil, [3]]
Expect [1, 2, 3]
Solution:
@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
@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 / 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 {

Injecting navigation behaviour into SwiftUI Views

This was a headscratcher so I'm pasting the code sample below where I got it to work.

Context

Suppose I have a view that will sit somewhere in my navigation stack, called SourceView. From the perspective of the wider application, this view has some buttons or actions that should trigger navigation to one or more other views, which we'll call TargetView1 and TargetView2.

The basic SwiftUI way of achieving this is to make SourceView aware of this navigation requirement, and implementing the navigation inside SourceView, by embedding a NavigationLink in its view hierarchy. A NavigationLink is a visible UI component akin to a button that a user can tap.

@phlippieb
phlippieb / CalendarDate.swift
Created November 22, 2024 18:26
Ergonomics for treating Swift Dates at day-specific granularity
import Foundation
/// Provides ergonomics for treating Swift Dates at day-specific granularity
struct CalendarDate {
let date: Date
private var calendar: Calendar { .current }
}
// MARK: Creating instances -