Skip to content

Instantly share code, notes, and snippets.

enum Fetched<T> {
case notFetched
case fetched(T)
}
#!/usr/bin/env bash
project=.;
objc=`grep -r "^@implementation " $project | wc -l | tr -d ' '`;
swift=`grep -r "^class " $project | wc -l | tr -d ' '`;
ratio=$(bc <<< "scale=3; $swift / $objc");
ratio_perc=$(bc <<< "$ratio * 100");
echo "Swift classes: $swift";
@joshavant
joshavant / UIView+Utility.swift
Created November 17, 2018 07:40
Ambiguity Treadmill
extension UIView {
@objc func exerciseAmbiguityInLayoutRepeatedly() {
if self.hasAmbiguousLayout {
Timer.scheduledTimer(timeInterval: 0.5,
target: self,
selector: #selector(UIView.exerciseAmbiguityInLayout),
userInfo: nil,
repeats: true)
}
}
//
// KeyboardInsettable.swift
//
// Created by Josh Avant on 9/11/18.
//
import Foundation
import UIKit
protocol KeyboardInsettable {
@joshavant
joshavant / refresh_cameras.sh
Created July 9, 2018 17:17
macOS Refresh Cameras Script
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Killing VDCAssistant..."
sudo killall VDCAssistant
echo "Killing AppleCameraAssistant..."
import Foundation
enum Result<T,U> {
case firstType(T)
case secondType(U)
}
enum Validation<T,Error> {
case valid(T)
case invalid(Error)
enum BirthdateValidation {
case valid(Date)
case missingComponents(Set<Birthdate.Component>)
case notOldEnough
init(date: Birthdate?) {
// implement validation for all states
}
}
@joshavant
joshavant / UITextView+HeightCalculation.swift
Created July 21, 2016 03:00
UITextView Height Calculation
extension UITextView {
// Note: This will trigger a text rendering!
func calculateViewHeightWithCurrentWidth() -> CGFloat {
let textWidth = self.frame.width -
self.textContainerInset.left -
self.textContainerInset.right -
self.textContainer.lineFragmentPadding * 2.0 -
self.contentInset.left -
self.contentInset.right
(lldb) po self.tableView.visibleCells
0 elements
(lldb) po self.tableView.indexPathsForVisibleRows
▿ Optional<Array<NSIndexPath>>
▿ Some : 3 elements
- [0] : <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
- [1] : <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}
- [2] : <NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}
if context == &FooKVOContext {
// .CGSizeValue can cause a sneaky segfault here, because it will execute on ALL keyPaths (not just "contentSize")
if let newContentSize = change?[NSKeyValueChangeNewKey]?.CGSizeValue() where keyPath == "contentSize" {
// do things
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}