Skip to content

Instantly share code, notes, and snippets.

http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=bpxgzkd94pjqxsym4dgxe8ww
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 / 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)
@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
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 / 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()
}))
@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.
// 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 / QuickExtensions.swift
Created March 17, 2016 00:49
Make Quick & Nimble tests much more pleasant in Swift
//
// QuickExtensions.swift
// PlanLib
//
// Created by Russ Bishop on 3/16/16.
// Copyright © 2016 PlanGrid. All rights reserved.
// MIT Licensed, use freely.
import Foundation
import Quick

Build Swift first using utils/build-script -R, other settings will need different directories below. Add these lines to ~/.bashrc, call with run-test path/to/test:

LLVM_DIR="/path/to/Swift/llvm"
SWIFT_BUILD_DIR="/path/to/Swift/build/Ninja-ReleaseAssert/swift-macosx-x86_64"

run-validation-test() {
    $LLVM_DIR/utils/lit/lit.py -a --param swift_site_config=$SWIFT_BUILD_DIR/validation-test-macosx-x86_64/lit.site.cfg $1
}