Skip to content

Instantly share code, notes, and snippets.

@sharplet
sharplet / Example.swift
Last active May 11, 2019 13:00
Mutable property lenses for ReactiveSwift with key paths
struct User {
var name: String
}
let currentUser = MutableProperty(User(name: "Foo"))
currentUser[\.name] <~ nameTextField.reactive.textValues
@sharplet
sharplet / LineSequence.swift
Created November 8, 2017 20:03
Getting a sequence of lines from a Swift string
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self
@sharplet
sharplet / Example.swift
Created November 3, 2017 17:46
An improved wrapper for UIImagePickerController
// Here is a usage example. Refer to ImagePicker.swift below for the implementation!
// 1. Easily configure the picker
let cameraPicker = ImagePicker(sourceType: .camera)
let cropPicker = ImagePicker(sourceType: .photoLibrary, allowsEditing: true)
// Automatically includes both kUTTypeImage and kUTTypeLivePhoto
let livePhotoPicker = ImagePicker(sourceType: .photoLibrary, mediaTypes: [.livePhotos])
// 2. Use the picker
@sharplet
sharplet / SimpleNetworkingExample.swift
Created September 14, 2017 21:37
Example Source Code for "A Simple Approach to Thread-Safe Networking in iOS Apps"
import Foundation
import PlaygroundSupport
enum URLResult {
case response(Data, URLResponse)
case error(Error, Data?, URLResponse?)
}
extension URLSession {
@discardableResult
@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
}
@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 / 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 / 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 / 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 / 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