Skip to content

Instantly share code, notes, and snippets.

View prachigauriar's full-sized avatar

Prachi Gauriar prachigauriar

View GitHub Profile
@prachigauriar
prachigauriar / NSCondition-WaitUntilDate-Example.m
Last active December 31, 2018 08:43
An example of using -[NSCondition waitUntilDate:].
//
// NSCondition-WaitUntilDate-Example.m
// NSConditionExample
//
// Created by Prachi Gauriar on 12/23/2013.
// Copyright (c) 2013 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@prachigauriar
prachigauriar / QLCDispatchAsyncWithCompletionAndTimeout.m
Last active May 28, 2022 18:23
Examples in Objective-C and Swift that use dispatch_async and dispatch_semaphore to cleanly replicate the behavior of ‑[NSCondition waitUntilDate:].
//
// QLCDispatchAsyncWithCompletionAndTimeout.m
// Blocks-WaitUntilDate
//
// Created by Prachi Gauriar on 12/23/2013.
// Copyright (c) 2013 Prachi Gauriar.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@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>
#!/bin/sh
plutil -convert json -r -o - - | python -m json.tool
@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>
@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 / 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 / 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
import Foundation
public enum Result<Value> {
case success(Value)
case failure(Error)
}
// Parent Protocol
public protocol Handler {
@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: