Skip to content

Instantly share code, notes, and snippets.

@russbishop
russbishop / OptionalFlatten.swift
Last active October 2, 2016 00:50
Flatten a Type?? -> Type? in Swift
// Russ Bishop
// PlanGrid
// MIT licensed, use freely.
public protocol OptionalType {
associatedtype WrappedType
init()
}
extension Optional: OptionalType {
// Working around Foundation deficiencies when trying to append to a file
// (and without run loops, delegates, and other BS).
// Why is this so complicated?
import Foundation
public final class TemporaryFile {
public let handle: NSFileHandle //IUO until Swift 2.2
public let url: NSURL
@russbishop
russbishop / NSURLBackgroundSessionBits.m
Created November 2, 2015 23:11 — forked from dtorres/NSURLBackgroundSessionBits.m
Storing Info in background requests.
/*
Basically what we are going to do is use and profit from NSURLRequests being conformant to NSCoding,
and a little known API from NSURLProtocol which allows us attach info to requests.
*/
//Step 0: For the purpose of this gist, we'll already have a background session setup and assume a bunch of stuff.
NSURLSession *bgSession = [NSURLSession magicMethodWhichGivesMeTheAlreadySetupSession]; //Geeez, Methods are long in Obj-C.
//IMPORTANT: Request must be mutable in order for this to work. Got an immutable one. Make a copy. Can't? Well, Make it so!.
//Step 1: Setup your basic request.
@russbishop
russbishop / Sequence.swift
Last active August 29, 2015 14:27
Proof that a lazy sequence is a stream and is only iterated once even with two filters
var sequencePoint: Int32 = 0
func printSequencePoint(context: String) {
print("\(context): \(OSAtomicIncrement32(&sequencePoint))")
}
var g = (0..<10).generate()
let seq = lazy(AnySequence(anyGenerator { _ -> Int? in
printSequencePoint("generator")
return g.next()
}))
var g = (0..<10).generate()
let seq = lazy(AnySequence(anyGenerator { _ -> Int? in
print("called") //prints "called" 11 times
return g.next()
}))
.filter { $0 != 2 }
.filter { $0 != 4 }
for x in seq {
print("item: \(x)")
@russbishop
russbishop / ReactiveCocoa+PlanGrid.swift
Created August 7, 2015 00:42
RAC 3.0 Swift Extensions
//
// ReactiveCocoa+PlanGrid.swift
// PlanGrid
//
// Created by Russ Bishop on 7/28/15.
// Copyright (c) 2015 PlanGrid. All rights reserved.
//
import Foundation
import ReactiveCocoa
@russbishop
russbishop / QueueTests.swift
Last active August 29, 2015 14:25
Sketchy queue deadlock detection?
private var queueContextKey = UnsafeMutablePointer<Void>.alloc(1)
private var queueContextValue = UnsafeMutablePointer<Void>.alloc(1)
private var mainQueueContextValue = UnsafeMutablePointer<Void>.alloc(1)
class QueueTest {
private let onQueue: dispatch_queue_t
init(useBackgroundQueue: Bool = false) {
if useBackgroundQueue {
let queueAttribs = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)
typealias verFunc = @convention(c) ()->UnsafePointer<CChar>
let libz = dlopen("libz.dylib", RTLD_LAZY)
let ret = dlsym(libz, "zlibVersion")
let zLibVersion = unsafeBitCast(ret, verFunc.self)
print("\(String.fromCString(zLibVersion())!)")
@russbishop
russbishop / StringExtensions.swift
Last active March 29, 2018 12:45
Swift.String truncate
import Foundation
public enum TruncateMode: Int {
case Head = 0
case Middle = 1
case Tail = 2
}
public extension String {
func nilIfEmpty() -> String? {
http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=bpxgzkd94pjqxsym4dgxe8ww