Skip to content

Instantly share code, notes, and snippets.

@dfrobison
dfrobison / SwiftUIPrint.swift
Last active November 29, 2022 17:30
Debug printing from swiftUI
extension View {
func debugPrint(_ value: Any) -> some View {
#if DEBUG
print (value)
#endif
return self
}
// Example usage
struct ContentView: View {
@dfrobison
dfrobison / Operators.swift
Created March 16, 2022 00:56
Functional programming: pipe forward and forward composition
func incr(_ x: Int) -> Int {
return x + 1
}
func square(_ x: Int) -> Int {
return x * x
}
precedencegroup ForwardApplication {
associativity: left
@dfrobison
dfrobison / ClockView.swift
Created January 25, 2022 20:47
SwiftUI ClockView
// Author: SwiftUI-Lab (www.swiftui-lab.com)
// Description: This code is part of the "Advanced SwiftUI Animations - Part 5"
// Article: https://swiftui-lab.com/swiftui-animations-part5/
import SwiftUI
struct ContentView: View {
var body: some View {
ClockView()
.background(.gray)
@dfrobison
dfrobison / FuzzyTextSearch.swift
Created November 5, 2020 23:09
[A weighted base text search] This is a swift version of Forrest Smith's fuzzy text search. I'm absolutely sure there are performance optimizations to be made to make it faster, but it's suitable for my needs. You can find the article here: https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
struct FuzzyTextSearch {
private static let maxMatches = 256
private static let defaultMatches: [Int] = Array(repeating: -1, count: maxMatches)
private static let recursionLimit = 10
private static let sequentialBonus = 15 // bonus for adjacent matches
private static let separatorBonus = 30 // bonus if match occurs after a separator
private static let camelBonus = 30 // bonus if match is uppercase and prev is lower
private static let firstLetterBonus = 15 // bonus if the first letter is matched
private static let leadingLetterPenalty = -5 // penalty applied for every letter in str before the first match
private static let maxLeadingLetterPenalty = -15 // maximum penalty for leading letters
@dfrobison
dfrobison / ChildSizeReader.swift
Created October 30, 2020 20:33
[Get the size of a child view ]
struct ChildSizeReader<Content: View>: View {
@Binding var size: CGSize
let content: () -> Content
var body: some View {
ZStack {
content()
.background(
GeometryReader { proxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: proxy.size)
@dfrobison
dfrobison / OnChangeBinding.swift
Created October 13, 2020 21:01
[Move onChange into binding]
import SwiftUI
struct ContentView: View {
@State private var rating = 0.0
var body: some View {
Slider(value: $rating.onChange(sliderChanged))
}
func sliderChanged(_ value: Double) {
//Like we mentioned earlier, different locales separate decimals and thousands with a different symbol. Most English people will use commas for thousands, whereas in Spanish speaking countries we normally separate with periods.
//This can be done correctly with NumberFormatter.
let numFormatter = NumberFormatter()
numFormatter.numberStyle = .decimal
numFormatter.string(from: 32.823) // 32.823 (English), 32,823 (Spanish)
//We can also format lists. Suppose you want to say “Apples, eggs, and pears” when you have the following array:
let items = ["apples", "eggs", "pears"]
//You can probably think of a clever way to concatenate the first two items only with a comma and then follow the last item with the word “and”. In fact doing so may not be complicated, but we have ListFormatter which allows us to do just that in just one line of code:
let items = ["apples", "eggs", "pears"]
//ListFormatter.localizedString(byJoining: items) // apples, eggs, and pears
//If you remove one of the elements, the formatter will do the right thing:
let items = ["apples", "pears"]
@dfrobison
dfrobison / LocalizingDateFormats.swift
Last active October 13, 2020 19:34
[Localizing Dates] By setting the format with setLocalizedDateFormatFromTemplate(_:) instead, the formatter will do the right thing based on the locale you give it.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy")
dateFormatter.string(from: Date()) // US: 09/27/2020
// ...
dateFormatter.locale = Locale(identifier: "es_BO")
dateFormatter.setLocalizedDateFormatFromTemplate("MMddyyyy")
dateFormatter.string(from: Date()) // Bolivia: 27/09/2020
@dfrobison
dfrobison / MenuPicker.swift
Last active October 9, 2020 02:34
[iOS 14 Menu Example]
public struct MenuPicker<T, V: View>: View {
@Binding var selected: Int
var array: [T]
var title: String?
let mapping: (T) -> V
public init(selected: Binding<Int>, array: [T], title: String? = nil,
mapping: @escaping (T) -> V) {
self._selected = selected