Skip to content

Instantly share code, notes, and snippets.

@algal
algal / StringFiltering.swift
Created November 3, 2016 19:23
Filter a string by a CharacterSet
// known-good: Xcode 8.1, Swift 3
extension String {
func keepingCharacters(inCharacterSet allowedCharacters:CharacterSet) -> String {
var filteredString:String = ""
var rangeToCheck:Range<String.Index> = self.startIndex..<self.endIndex
while true {
if let allowedCharRange = self.rangeOfCharacter(from: allowedCharacters,
options: [],
@algal
algal / PrintToStdErr.swift
Last active July 27, 2022 06:47
print to stderr in Swift 3
// known-good: Xcode 8, Swift 3
import Foundation
var standardError = FileHandle.standardError
extension FileHandle : TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
// kg: Xcode 8, Swift 3
extension FileHandle : TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
self.write(data)
}
}
// now something like:
// known-good. Xcode 8, Swift 3
import XCPlayground
/// writes string s to a file in the playground shared directory and returns its URL
func write(string s:String, toPlaygroundSharedDataDirectoryFileWithName name:String) -> URL?
{
let dirURL:URL = XCPlaygroundSharedDataDirectoryURL as URL
let fileURL:URL = dirURL.appendingPathComponent(name)
let path = fileURL.path
@algal
algal / OrientationHelper.swift
Last active August 30, 2016 15:45
What orientation value re-orients raw images?
import UIKit
import AVFoundation
/** Returns a UIImageOrientation that will adjust an image
- parameter deviceOrientation: physical orientation of the device when the image was captured. Must not be FaceUp or FaceDown
- parameter position: position of the camera used for capture. Must be .Front or .Back
- returns an UIImageOrientation, which can be used when initializing a UIImageView.
// overloading on function return values to
// enable destructuring a JSON-like type
import Foundation
enum JSONType {
case JBool(Bool)
case JNumber(Int)
case JString(String)
}
//
// SettledownTimer.swift
//
//
// Created by Alexis Gallagher
// known-good: Swift 2.2, Xcode 7.3.1
//
import Foundation
@algal
algal / update-carthage-managed-dependencies.sh
Created May 27, 2016 18:18
shell script & project policy summary use of Carthage for fetching source only (not building)
#!/bin/sh
command -v carthage >/dev/null 2>&1 || { echo >&2 "I require carthage but it's not installed. Aborting."; exit 1; }
carthage update --no-build
# A policy proposal:
#
# We use the "carthage" tool only to download the source code of
# third-party libraries that we depend on.
extension NSURL {
/// Appends path components to the receiver, optionally specifying if full path is a directory to avoid filesystem access
func URLByAppendingPathComponents(pathComponents:[String],lastIsDirectory:Bool? = nil) -> NSURL {
guard pathComponents.isEmpty == false else { return self }
var newURL = self
for idx in pathComponents.startIndex..<pathComponents.endIndex.predecessor() {
newURL = newURL.URLByAppendingPathComponent(pathComponents[idx], isDirectory: true)
}
if let last = pathComponents.last {
if let lastIsDirectory = lastIsDirectory {
// known-good: Xcode 7.3.1, Swift 2.2
/**
Filters to only those file URLs whose filenames match the glob pattern.
- parameter fileURLs: file URLs to filter
- parameter filenameGlobPattern: a glob pattern to match against the filename of the file pointed to by a file URL
*/
func URLsWithFilenamesMatchingGlobPattern(fileURLs:[NSURL],filenameGlobPattern:String) -> [NSURL]
{
let filenamePredicate = NSPredicate(format: "SELF like %@", argumentArray: [filenameGlobPattern])