Skip to content

Instantly share code, notes, and snippets.

@erica
erica / fortune.swift
Created April 14, 2015 17:14
Fortune script in Swift. Make sure to chmod +x before using.
#!/usr/bin/env xcrun swift
import Foundation
func Fortune() -> String? {
if let urlString = "http://iheartquotes.com/api/v1/random?format=json".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding),
url = NSURL(string:urlString),
data = NSData(contentsOfURL: url),
json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil) as? NSDictionary,
quote = json["quote"] as? String {
@erica
erica / gist:b0127c80e07da963b3aa
Last active August 29, 2015 14:19
Stupid Reflection Tricks
// My evil
extension Range {
var NSRangeValue : NSRange {
let location = reflect(startIndex).summary.toInt() ?? 0
let length = reflect(distance(startIndex, endIndex)).summary.toInt() ?? 0
return NSMakeRange(location, length)
}
}
let testString = "abcdefghij"
@erica
erica / gist:e7d45e13611a14dd79d6
Created April 21, 2015 16:42
More than anyone really wants to know about tuples and closures/functions
import UIKit
typealias Tupletype = (a:Int, b:String, c:Double)
let params1 = (1, "Params1", 2.4) // anonymous tuple
let params2 : Tupletype = (1, "Params2", 2.4) // named parameter tuple
// Standard tuple: var myclosure: (Int, String, Double) -> ()
var myclosure = {(a:Int, b:String, c:Double)->() in println("\(a) \(b) \(c)")}
myclosure(params1) // works
// myclosure(params2) // does not work
func ToArray<S : SequenceType>(sequence: S) -> Array<S.Generator.Element> {
return Array<S.Generator.Element>(sequence)
}
@erica
erica / gist:5820cb05acb58820ae37
Created April 24, 2015 20:22
Collection Randomizer
import Foundation
public struct RandomGenerator<C: CollectionType> : GeneratorType, SequenceType {
private var backingGenerator : PermutationGenerator<C, [C.Index]>
public init(_ elements : C) {
var indices = Array(elements.startIndex..<elements.endIndex)
for index in 0..<count(indices) {
var swapIndex = index + Int(arc4random_uniform(UInt32(count(indices) - index)))
if swapIndex != index {
swap(&indices[index], &indices[swapIndex])
public let diagnosticsPath = (NSProcessInfo.processInfo().environment["DYLD_INSERT_LIBRARIES"] as! String).stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.stringByAppendingPathComponent("AuxiliaryDiag.map")
import UIKit
import XCPlayground
// Please enable simulator to view results in playground
extension UIView {
class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(duration)
animations()
import UIKit
import XCPlayground
// Enable simulator to view results in playground
extension UIView {
class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(duration)
animations()
/// Fetch item by class
public func SelectTypedItemFromArray<T, U: SequenceType>(type t: T.Type, array: U) -> T? {
for eachItem in array {
if let typedItem = eachItem as? T {
return typedItem
}
}
return nil
}
public func CreateConvolve(kernel : [Int16]) -> UIImage -> UIImage? {
return {(image : UIImage) -> UIImage? in
let data = DataFromInt16Kernel(kernel)
return Convolve(image, data)}
}