Skip to content

Instantly share code, notes, and snippets.

@nanoxd
nanoxd / Collection+anySatisfy.swift
Created December 10, 2018 12:17
[Collection.anySatisfy] With the introduction of allSatisfy, we can cleanly represent the inverse.
extension Collection {
func anySatisfy(_ p: (Element) -> Bool) -> Bool {
return !self.allSatisfy { !p($0) }
}
}
@nanoxd
nanoxd / PermutationIterator.swift
Created December 10, 2018 00:49
[PermutationIterator] Use of Heap's Algorithm to create permutations
public struct PermutationIterator<T>: IteratorProtocol {
private var hasReturnedInitial = false
private var a: [T]
private var c: [Int]
private let n: Int
private var i = 0
public init<C: Collection>(_ values: C) where C.Element == T {
a = Array(values)
n = a.count
@nanoxd
nanoxd / run.sh
Last active June 10, 2019 19:44
[Xcode Diet] Clean up after Xcode's voracious hard drive appetite
#!/usr/bin/env sh
set -e
fancy_echo() {
local fmt="$1"; shift
printf "\n$fmt\n" "$@"
}
@nanoxd
nanoxd / fix-xcode
Last active March 15, 2019 12:17
Xcode Scripts
#!/usr/bin/env sh
# Xcode sometimes gets sad and won't index or other shenanigans. You can easily cheer up Xcode
# and get things happy again by simply removing some files. This little script does just that.
# Quit Xcode
osascript -e 'tell app "Xcode" to quit'
# Remove derived data
rm -rf ~/Library/Developer/Xcode/DerivedData/*
@nanoxd
nanoxd / lint
Created November 11, 2018 14:27
[bin/lint] Binary to run swiftlint, meant to be placed at the root of the project #sh
#!/bin/sh
#
# Lint any changed files using swiftlint.
#
# This works both independently and as a run phase script.
# Path to installed version of Swift lint.
readonly SWIFT_LINT_PATH=$(which swiftlint)
if [ -z "$SWIFT_LINT_PATH" ]; then
@nanoxd
nanoxd / FixBTSound.applescript
Last active November 11, 2018 14:26 — forked from mluisbrown/FixBTSound.applescript
[FixBTSound] AppleScript to set macOS audio input device to "Internal Microphone"
-- Sets your audio input source to "Internal Microphone"
-- Frequently needed if you use bluetooth headpohones and
-- run the Xcode iOS simulator, which will often set your
-- headphones to be the input device, resulting in a drastic
-- decrease in sound quality, and making it mono
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
@nanoxd
nanoxd / UIWindow+TopLevel.swift
Created October 31, 2018 00:20
[UIWindow+TopLevel] Add a new window that rests above the key window #swift #ios
extension UIWindow {
final class StatusBarPreferringViewController: UIViewController {
// MARK: - Inputs
private let statusBarStyle: UIStatusBarStyle
// MARK: - Initialization
init(statusBarStyle: UIStatusBarStyle)
self.statusBarStyle = statusBarStyle
@nanoxd
nanoxd / NSObject+AssociatedObject.swift
Created October 16, 2018 14:25
[NSObject+AssociatedObject] Wrapper around associated objects #swift
public enum AssociationPolicy {
case assign
case retain
case copy
case retainNonatomic
case copyNonatomic
fileprivate var policy: objc_AssociationPolicy {
switch self {
@nanoxd
nanoxd / HasCustomView.swift
Created October 16, 2018 14:14
[HasCustomView] Define a custom property for `UIViewController` to use a typed view #swift #ios
/// The HasCustomView protocol defines a customView property for UIViewControllers to be used in exchange of the regular view property.
/// In order for this to work, you have to provide a custom view to your UIViewController at the loadView() method.
public protocol HasCustomView {
associatedtype CustomView: UIView
}
extension HasCustomView where Self: UIViewController {
/// The custom typed view
public var customView: CustomView {
guard let customView = view as? CustomView else {
@nanoxd
nanoxd / DispatchQueue+isMain.swift
Created October 12, 2018 11:26
[DispatchQueue+isMain] Determine if the current GCD Queue is the main queue #swift
extension DispatchQueue {
fileprivate static let mainQueueKey = DispatchSpecificKey<()>()
static func configureMainQueue() {
main.setSpecific(key: mainQueueKey, value: ())
}
}
public extension DispatchQueue {
/// Easy and safe way of checking if the current queue is the main queue