Skip to content

Instantly share code, notes, and snippets.

/// An API for managing UIKit background tasks by composing signals.
@interface UIApplication (SHReactiveBackgroundTask)
/// Creates a signal that wraps an underlying signal in a background task.
/// Upon subscription, starts a background task, and then automatically ends
/// the background task when the underlying signal completes or errors.
/// If the background task expires, the subscription to the underlying signal
/// will automatically be disposed of.
///
/// @note If the call to `-beginBackgroundTaskWithExpirationHandler:` returns
@sharplet
sharplet / xcode_prebuilt_framework_debugger_crash.log
Created July 19, 2015 11:12
Xcode crash log when loading debug symbols from another machine
Process: Xcode [56151]
Path: /Applications/Xcode.app/Contents/MacOS/Xcode
Identifier: com.apple.dt.Xcode
Version: 6.3.2 (7718)
Build Info: IDEFrameworks-7718000000000000~2
App Item ID: 497799835
App External ID: 812404257
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Xcode [56151]
@sharplet
sharplet / lazy_reduce.rb
Created August 7, 2015 10:44
Lazy `#reduce` and `#join` in Ruby
require "rspec/autorun"
class Enumerator::Lazy
def reduce(*)
Lazy.new([nil]) do |yielder, _|
yielder << super
end
end
def join(separator="")
@sharplet
sharplet / git
Created August 21, 2015 03:44
Disable git at the terminal, but still allow shelling out
#!/bin/sh
if [ -t 1 ]; then
echo LOL >&2
exit 1
else
real_git=$(type -a git | head -2 | tail -1 | cut -d" " -f 3)
exec "$real_git" ${*}
fi
@sharplet
sharplet / swift_pros_and_cons.md
Created October 22, 2015 00:55
A list of pros and cons to adopting Swift on new projects

Swift Pros and Cons

Pros

  • Code tends to be shorter & clearer
  • Value types are a super valuable design pattern (see what I did there)
  • Optionals are amazing
  • Online resources tend to be in Swift these days
  • Swift 2 is a lot better than Swift 1.2
  • Trending towards stability
@sharplet
sharplet / GreetingFeature.swift
Created October 25, 2015 22:17
Cucumber inspired syntax for Xcode UI tests
final class GreetingFeature: Feature {
override func scenarios() {
Scenario("Greeting on first load")
.Given("the app has launched")
.Then("the text 'Hello, world!' is on screen")
}
}
@sharplet
sharplet / trap.swift
Created November 23, 2015 03:46
Simple signal handling in Swift
import Darwin
enum Signal: Int32 {
case HUP = 1
case INT = 2
case QUIT = 3
case ABRT = 6
case KILL = 9
case ALRM = 14
case TERM = 15
@sharplet
sharplet / Actor.swift
Last active April 5, 2017 07:54
Simple GCD-based actors in Swift
import Dispatch
/// Wraps some `Base` type so that all method calls become
/// "message sends", e.g., `async { $0.foo() }` or `sync { $0.bar() }`.
public final class Actor<Base> {
private var instance: Base
private let queue: DispatchQueue
public init(_ instance: Base, target: DispatchQueue? = nil) {
self.instance = instance
@sharplet
sharplet / scalars.swift
Last active April 27, 2021 19:03
Swift program to print out all the unicode scalars in a Foundation CharacterSet.
// Prints out all the unicode scalars in a Foundation CharacterSet.
//
// Compile: swiftc -O scalars.swift
// Run: ./scalars <character set name>
import Foundation
extension UnicodeScalar {
static var allScalars: AnySequence<UnicodeScalar> {
let numbers = sequence(first: 0, next: { $0 + 1 })
@sharplet
sharplet / BitMask.swift
Created July 11, 2017 15:50
Playing around with a high-level API for working with bit masks
struct BitMask<T: FixedWidthInteger>: RandomAccessCollection {
typealias SubSequence = BitMask<T>
var rawValue: T {
var rawValue: T = 0
for bit in self {
rawValue |= bit
}
return rawValue
}