Skip to content

Instantly share code, notes, and snippets.

@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)
}
}
@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
@joshavant
joshavant / String+Replacement.swift
Created May 1, 2016 00:19
Replace characters from set with replacement string
import Foundation
extension String {
func replaceCharactersFromSet(characterSet: NSCharacterSet, replacementString: String) -> String {
let scanner = NSScanner(string: self)
scanner.charactersToBeSkipped = nil
let sanitizedString = NSMutableString(capacity: self.characters.count)
while(!scanner.atEnd) {
enum Fetched<T> {
case notFetched
case fetched(T)
}
@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 UIKit
func namedModels() -> [ModelObject] {
let names: [String] = UIFont.familyNames()
return names.map(ModelObject.init)
}
class ModelObject: NSObject {
let name: String
init(name: String) { self.name = name }
#!/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 / NibLoadable.swift
Created October 9, 2015 21:08
NibLoadableView
import UIKit
protocol NibLoadable {
func setupFromXib()
}
// This extension loads a .xib file with the same filename as the class name
// i.e. `class FoobarHeaderView` -> loads FoobarHeaderView.xib
extension NibLoadable where Self: UIView {
func setupFromXib() {
//
// KeyboardInsettable.swift
//
// Created by Josh Avant on 9/11/18.
//
import Foundation
import UIKit
protocol KeyboardInsettable {
import Foundation
enum Result<T,U> {
case firstType(T)
case secondType(U)
}
enum Validation<T,Error> {
case valid(T)
case invalid(Error)