Skip to content

Instantly share code, notes, and snippets.

View atetlaw's full-sized avatar
💻
Working on something cool

Andrew Tetlaw atetlaw

💻
Working on something cool
View GitHub Profile
@atetlaw
atetlaw / MapVoid.swift
Created January 20, 2022 06:53
A handy Combine operator if you are only interested in a publisher completing: `MapVoid`
import Combine
extension Publisher {
func mapVoid() -> MapVoid<Self> {
MapVoid(upstream: self)
}
}
struct MapVoid<P: Publisher>: Publisher {
typealias Output = Void
@atetlaw
atetlaw / CardSheet.swift
Last active January 8, 2022 09:07
A simple pure SwiftUI card-style action sheet, that also blurs the background content, with animated transition
//
// CardSheet.swift
//
// Created by Andrew Tetlaw on 4/12/21.
//
import SwiftUI
struct CardSheet<SheetContent: View>: ViewModifier {
@Binding var show: Bool
@atetlaw
atetlaw / characters.swift
Created September 26, 2019 02:26
List characters in a CharacterSet (Swift 5)
// From https://stackoverflow.com/questions/34772439/see-characters-in-an-nscharacterset-swift
extension NSCharacterSet {
var characters: [String] {
var chars = [String]()
for plane:UInt8 in 0...16 {
if self.hasMemberInPlane(plane) {
let p0 = UInt32(plane) << 16
let p1 = (UInt32(plane) + 1) << 16
for c:UTF32Char in p0..<p1 {
if self.longCharacterIsMember(c) {
@atetlaw
atetlaw / LocationExtensions.swift
Created February 12, 2019 03:30
Trying to extend CoreLocation types to provide `Measurement` values. Of course all of these types just `typealias` `Double`, so they're all extending `Double`!
extension CLLocationDegrees {
public var degrees: Measurement<UnitAngle> {
return Measurement(value: self, unit: .degrees)
}
public var radians: Measurement<UnitAngle> {
return degrees.converted(to: .radians)
}
public var degreesMinutes: (Measurement<UnitAngle>, Measurement<UnitAngle>) {
@atetlaw
atetlaw / Namespace.swift
Last active September 3, 2016 17:14
This is a mad attempt to create collection of protocols and extensions that you can use to make your namespaced constants output their fully qualified names as strings, no matter how long the namespace. It makes use of associated types to link the nested constant elements in reverse.
public protocol ScopeNamed {
static func scope() -> String
}
/* Named represents the basic element, something that can output its name, but also has a parent scope */
public protocol Named {
associatedtype ParentScope: ScopeNamed
}
@atetlaw
atetlaw / bumblebee.swift
Last active September 3, 2016 04:46
Bumblebee operator ~% for getting rawValue from any RawRepresentable enum
prefix operator ~% {}
prefix func ~%<E: RawRepresentable>(e: E) -> E.RawValue {
return e.rawValue
}
@atetlaw
atetlaw / SequenceType+CategorySupport.swift
Last active September 1, 2015 10:06
Swift extensions are cool. You can add special support to all SequenceTypes for your types.
extension SequenceType where Generator.Element == Category {
func joinNamesWithSeparator(separator: String) -> String {
return self.reduce("", combine: { ( combinedString: String, cat: Category) in
guard let name = cat.name else {
return combinedString
}
return combinedString + (combinedString.isEmpty ? name : "\(separator)\(name)")
})
}
}
@atetlaw
atetlaw / AssertThrows.swift
Last active September 11, 2015 03:34
Simple `AssertThrows` and `AssertNoThrow` for unit testing using XCTest in Swift
public func AssertThrowsSpecific<E:ErrorType>(@autoclosure expression: () throws -> Void, expectedError: E, _ message: String = "", file: String = __FILE__, line: UInt = __LINE__) {
do {
try expression()
FailAssertion(__FUNCTION__, message, file, line)
} catch let caughtError {
guard caughtError is E else {
FailAssertion(__FUNCTION__, message, file, line)
return
}
}
@atetlaw
atetlaw / SegueNaming.swift
Last active September 11, 2015 03:36
A silly Swift protocol so you can use an Enum's values to identify storyboard segues
import UIKit
protocol SegueNaming: RawRepresentable {
static func handle<T : SegueNaming>(segue: UIStoryboardSegue, f: T -> Void)
init?(withSegue: UIStoryboardSegue)
var segueIdentifier: String { get }
}
extension SegueNaming where Self.RawValue == String {
static func handle<T:SegueNaming>(segue: UIStoryboardSegue, f: T -> Void) {

Keybase proof

I hereby claim:

  • I am atetlaw on github.
  • I am atetlaw (https://keybase.io/atetlaw) on keybase.
  • I have a public key whose fingerprint is C884 BC45 DD40 2CA6 17EA 9D07 081C 08B6 B5DD 33FC

To claim this, I am signing this object: