Skip to content

Instantly share code, notes, and snippets.

View prachigauriar's full-sized avatar

Prachi Gauriar prachigauriar

View GitHub Profile
@prachigauriar
prachigauriar / FormattedTextFieldView.swift
Last active January 19, 2023 20:58
Trying (and failing) to use formatters SwiftUI TextFields
import Foundation
import SwiftUI
struct FormattedTextFieldView : View {
@State private var double = 0.5
@State private var nsNumber: NSNumber = 0.5
static let numberFormatter: NumberFormatter = {
@prachigauriar
prachigauriar / Slider+LogarithmicScale.swift
Last active October 16, 2024 22:33
SwiftUI Slider with Logarithmic Scale
extension Binding where Value == Double {
/// Returns a new version of the binding that scales the value logarithmically using the specified base. That is,
/// when getting the value, `log_b(value)` is returned; when setting it, the new value is `pow(base, newValue)`.
///
/// - Parameter base: The base to use.
func logarithmic(base: Double = 10) -> Binding<Double> {
Binding(
get: {
log10(self.wrappedValue) / log10(base)
},
@prachigauriar
prachigauriar / gist:b276bdf026606730b6d9b0b718f5c5ee
Created January 2, 2018 19:36
Backtrace with main thread hang on CFPREFERENCES_IS_WAITING_FOR_SYSTEM_CFPREFSD
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x0000000185297568 libsystem_kernel.dylib`mach_msg_trap + 8
frame #1: 0x00000001852973e0 libsystem_kernel.dylib`mach_msg + 72
frame #2: 0x0000000107482710 libdispatch.dylib`_dispatch_mach_send_and_wait_for_reply + 644
frame #3: 0x0000000107482bbc libdispatch.dylib`dispatch_mach_send_with_result_and_wait_for_reply + 56
frame #4: 0x0000000185407d7c libxpc.dylib`xpc_connection_send_message_with_reply_sync + 196
frame #5: 0x00000001857135a8 CoreFoundation`__78-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:]_block_invoke + 24
frame #6: 0x0000000185741ed4 CoreFoundation`CFPREFERENCES_IS_WAITING_FOR_SYSTEM_CFPREFSD + 48
frame #7: 0x00000001857134ec CoreFoundation`-[CFPrefsPlistSource sendRequestNewDataMessage:toConnection:retryCount:error:] + 256
frame #8: 0x0000000185711640 CoreFoundation`-[CFPrefsPlistSource handleErrorReply:fromMessageSettingKey:toValue:retryCount:
import Foundation
public enum Result<Value> {
case success(Value)
case failure(Error)
}
// Parent Protocol
public protocol Handler {
@prachigauriar
prachigauriar / JSONDecoder-Decimal.swift
Last active February 21, 2018 22:51
JSONDecoder Decimal Error
import Foundation
struct DoubleItem : Codable {
var name: String
var price: Double
}
struct DecimalItem : Codable {
var name: String
var price: Decimal
@prachigauriar
prachigauriar / NextDay.swift
Created April 12, 2017 18:46
Calendrical Calculations
import Foundation
extension Calendar {
func startOfNextDay(for date: Date) -> Date? {
let start = startOfDay(for: date)
return self.date(byAdding: .day, value: 1, to: start)
}
func endOfDay(for date: Date) -> Date? {
@prachigauriar
prachigauriar / ErrorBox.swift
Created August 10, 2016 03:57
ErrorBox for Xcode 8b4 and Xcode 8b5
import Foundation
/// Example error
enum MyError : Error {
case error1
}
/// Boxes error in a struct
struct ErrorBox<BoxedError: Error> {
@prachigauriar
prachigauriar / SynchronousSubworkflowTask.h
Last active August 29, 2015 14:12
Synchronous Subworkflow Task
//
// SynchronousSubworkflowTask.h
// SynchronousSubworkflowTask
//
// Created by Prachi Gauriar on 1/5/2015.
// Copyright (c) 2015 Prachi Gauriar. All rights reserved.
//
#import <Task/Task.h>
#!/bin/sh
plutil -convert json -r -o - - | python -m json.tool
@prachigauriar
prachigauriar / QLCReverseStrings.m
Last active November 24, 2020 08:54
Code that reverses NSStrings using three different methods. Requires the `URLMock/TestHelpers` pod to generate random Unicode strings.
//
// main.m
// ReverseString
//
// Created by Prachi Gauriar on 3/23/2014.
// Copyright (c) 2014 Prachi Gauriar. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <URLMock/UMKTestUtilities.h>