Skip to content

Instantly share code, notes, and snippets.

@mattt
mattt / UIViewControllerPreview.swift
Last active January 8, 2024 23:09
Generic structures to host previews of UIView and UIViewController subclasses.
import UIKit
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable {
let viewController: ViewController
init(_ builder: @escaping () -> ViewController) {
viewController = builder()
}
import Foundation
extension Character {
var isEmoji: Bool {
return unicodeScalars.allSatisfy { $0.properties.isEmoji }
}
}
func recentlyUsedEmoji() -> [Character]? {
#if os(iOS)
@mattt
mattt / bridge.swift
Last active June 14, 2023 11:47
Example usage of @mxcl's swift-sh
#!/usr/bin/swift sh
import DeckOfPlayingCards // @NSHipster ~> 4.0.0
import PlayingCard
import Cycle // @NSHipster == bb11e28
class Player {
var name: String
var hand: [PlayingCard] = []

Pitch: Unicode Named Character Escape Sequence

Introduction

This proposal adds a new \N{name} escape sequence to Swift string literals, where name is the name of a Unicode character.

Discussion

The Unicode named character escape sequence was previously discussed here:

import Foundation
extension Collection where Element: Comparable {
func sorted(ascending: Bool = true, using comparator: (Element) -> (Element) -> ComparisonResult) -> [Element] {
return self.sorted { lhs, rhs in
comparator(lhs)(rhs) == (ascending ? .orderedAscending : .orderedDescending)
}
}
}
import Foundation
extension UnitConcentrationMass {
class var microgramsPerCubicMeter: UnitConcentrationMass {
let converter = UnitConverterLinear(coefficient: 1e-9)
return .init(symbol: "µg/m³", converter: converter)
}
}
func unzip<S: Sequence, T, U>(_ sequence: S) -> (AnySequence<T>, AnySequence<U>)
where S.Element == (T, U)
{
return (
AnySequence(sequence.lazy.map{ $0.0 }),
AnySequence(sequence.lazy.map{ $0.1 })
)
}
let fibonacci = sequence(first: (0, 1), next: { ($1, $0 + $1) })
/*
# Problem
In Swift, it can be cumbersome to work with Unicode characters that are
non-printing, confusable, or have difficulty rendering in the editor.
For example, to generate the "Family: Woman, Girl" emoji:
*/
// Option 1: Unicode Scalar Value Escapes
import Foundation
struct TruthyValue {
var value: Bool?
init(_ value: Bool?) {
self.value = value
}
init(_ value: Int) {
switch value {
public struct BoundedSequence<Base>: Sequence, IteratorProtocol where Base: Sequence {
public struct Boundary: Equatable {
public let isStart: Bool
public let isEnd: Bool
}
private var _iterator: Base.Iterator
private var _previous: Base.Element?
private var _current: Base.Element?
private var _next: Base.Element?