Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
SlaunchaMan / After.swift
Created June 17, 2015 18:38
Getting the Next Element in a Swift Array
extension Array {
func after(item: T) -> T? {
if let index = find(self, item) where index + 1 < count {
return self[index + 1]
}
return nil
}
}
@SlaunchaMan
SlaunchaMan / CLKTextProvider+NNNCompoundTextProviding.h
Created August 24, 2015 04:06
Getting around Swift 2’s lack of compatibility with vararg methods for complication text providers.
@interface CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString;
@end
@SlaunchaMan
SlaunchaMan / random.swift
Created August 25, 2020 19:45
Random numbers adding up to a sum
import Foundation
func randomNumbers(_ count: Int, totalling sum: Int) -> [Int] {
let randomOffsets = (0 ..< count - 1).map { _ in Int.random(in: 1 ..< sum) }
let range = ([0] + randomOffsets + [sum]).sorted()
var values: [Int] = []
for i in 0 ... count - 1 {
@SlaunchaMan
SlaunchaMan / SwiftUIStackView.swift
Created June 16, 2020 14:03
A SwiftUI-like function builder approach for UIStackView.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
@_functionBuilder
struct UIViewBuilder {
static func buildBlock(_ views: UIView...) -> [UIView] {
return views
}
import Foundation
let cast = ["🚶‍♂️🚶🏻‍♂️🚶🏼‍♂️🚶🏽‍♂️🚶🏾‍♂️🚶🏿‍♂️🚶‍♀️🚶🏻‍♀️🚶🏼‍♀️🚶🏽‍♀️🚶🏾‍♀️🚶🏿‍♀️"]
var string = "Virtual #WWDC20 queue:\n"
while string.count <= 280 {
string.append(cast.randomElement()!)
}
@SlaunchaMan
SlaunchaMan / DynamicSubclassGenerator.swift
Created January 14, 2020 03:05
Generate dynamic subclasses of Objective-C objects in Swift
import Foundation
import ObjectiveC
private var subclassCounts: [String: Int] = [:]
func GenerateDynamicSubclass<T: NSObject>(of class: T.Type) -> T.Type! {
let oldClassName = NSStringFromClass(`class`) as String
var subclassCount = subclassCounts[oldClassName] ?? 0
let className = "\(oldClassName)_DynamicSubclass_\(subclassCount)"
@SlaunchaMan
SlaunchaMan / Locale+DistanceUnit.swift
Last active October 3, 2019 19:25
Determining which Locales use miles for distance formatting.
import Foundation
import MapKit
extension Locale {
var usesMilesForDistanceFormatting: Bool {
guard usesMetricSystem else { return true }
let miles = Measurement(value: 42, unit: UnitLength.miles)
let kilometers = miles.converted(to: .kilometers)
@SlaunchaMan
SlaunchaMan / FetchPublisher.swift
Last active June 21, 2019 05:11
FetchPublisher
//
// FetchPublisher.swift
// [REDACTED]
//
// Created by Jeff Kelley on 6/20/19.
// Copyright © 2019 Jeff Kelley. All rights reserved.
//
import CoreData
import Combine
@SlaunchaMan
SlaunchaMan / build.sh
Last active February 21, 2019 08:34
Enumerate all schemes in a project, archive them, and export as IPAs.
#!/bin/bash
# Builds all targets and places IPAs in build/
# Constants
SIGNING_IDENTITY="iPhone Distribution: Detroit Labs, LLC"
# Get a list of all schemes, then build each.
xcodebuild -project PROJECT.xcodeproj -list | \
sed -n '/Schemes/,/^$/p' | \
@SlaunchaMan
SlaunchaMan / no_u.swift
Last active February 8, 2019 01:16
I wanted a version of zip(_:_:) that padded the end of the shorter sequence with Optionals.
import Foundation
func paddedZip<T, U, Sequence1, Sequence2>(
_ sequence1: Sequence1,
_ sequence2: Sequence2
) -> Zip2Sequence<[T?], [U?]> where
Sequence1 : Sequence, Sequence1.Element == T,
Sequence2 : Sequence, Sequence2.Element == U {
var array1: [T?] = []
var array2: [U?] = []