This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Russ Bishop | |
// PlanGrid | |
// MIT licensed, use freely. | |
public protocol OptionalType { | |
associatedtype WrappedType | |
init() | |
} | |
extension Optional: OptionalType { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ReactiveCocoa+PlanGrid.swift | |
// PlanGrid | |
// | |
// Created by Russ Bishop on 7/28/15. | |
// Copyright (c) 2015 PlanGrid. All rights reserved. | |
// | |
import Foundation | |
import ReactiveCocoa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())!)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public enum TruncateMode: Int { | |
case Head = 0 | |
case Middle = 1 | |
case Tail = 2 | |
} | |
public extension String { | |
func nilIfEmpty() -> String? { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=bpxgzkd94pjqxsym4dgxe8ww | |
NewerOlder