Skip to content

Instantly share code, notes, and snippets.

import UIKit
import XCPlayground
import Social
// Fetch image and convert JPEG data to base64 representation
let image = UIImage(named:"Quote")!
let base64String = UIImageJPEGRepresentation(image, 1.0).base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
// Establish post dictionary
var post = [NSObject:AnyObject]()
// Take 4 (aka the akempgen approach):
var fibGenerator = GeneratorOf{_ -> Int? in fibs.append(fibs.reduce(0, combine:+)); return fibs.removeAtIndex(0)}
// Take 3
var fibGenerator = GeneratorOf{_ -> Int? in
fibs.append(fibs.reduce(0, combine:+))
return fibs.removeAtIndex(0)
}
// Take 2
func Scramble<C: MutableCollectionType where C.Index == Int>(var collection: C) -> C {
let elementCount = count(collection)
for index in dropLast(Array(0..<elementCount)) {
let r = index + Int(arc4random_uniform(numericCast(elementCount - index)))
swap(&collection[index], &collection[r])
}
return collection
}
public struct StderrOutputStream: OutputStreamType {
// Support for file redirection back to console
private static var fpos : fpos_t = 0
private static var fd = dup(fileno(stderr))
private static var isRedirected_ = false
public static var isRedirected : Bool {return isRedirected_}
// When false, the output file is rewritten and not appended
private static var appendToFile = true
extension String {
var utf8Representation : String {
var utf8String = ""
for c in self.unicodeScalars {
let inputArray : [UInt32] = [c.value]
var outputArray = [UInt8]()
var output = SinkOf<UInt8>{outputArray.append($0)}
let error = transcode(UTF32.self, UTF8.self, inputArray.generate(), &output, stopOnError: true)
if !error {
let s = String(bytes:outputArray, encoding:NSUTF8StringEncoding)!
@erica
erica / gist:8fe5854c939078bf0573
Last active August 29, 2015 14:21
Enums: They're not just for breakfast anymore
// Enums. They're not just for breakfast anymore.
// This is a simple enumeration that's based on numeric values,
// just like you'd use in many other programming languages
enum Dwarf : Int {
case Bashful = 1, Doc, Dopey, Grumpy, Happy, Sleepy, Sneezy
}
// And instantiate them. Hi Sleepy.
var d : Dwarf = .Sleepy // shows (Enum Value) in gutter
println(d) // initially prints (Enum Value) in console until you add the following extension (so much for playground's top to bottom evaluation guarantee)
Monday 2:30 PM Platforms State of the Union
Dev Tools
Tues 1:30 PM
Tues 2:30 PM
Tues 3:30 PM Improving your Existing Apps with Swift
Weds 9:00 AM
Weds 10:00 AM
@erica
erica / gist:f988a7c7f9740d9efd28
Last active August 29, 2015 14:22
Messing with raw option sets
import UIKit
struct BitFlagSet: RawOptionSetType, BooleanType, Printable, DebugPrintable {
// Settable bit count applies to all instances of the struct
private static var bitCount_ : UInt = 8
static var bitCount : UInt {get{return bitCount_} set{bitCount_ = min(max(newValue, 1), 64)}}
static var allOnes : BitFlagSet {return BitFlagSet(rawValue:(bitCount_ < 64 ? UInt(1 << bitCount_) - 1 : UInt.max))}
static var allZeros: BitFlagSet {return BitFlagSet(rawValue:0)}
for appID in appIDs {
let urlString = "https://itunes.apple.com/lookup?id=" + appID
if let
url = NSURL(string:urlString),
data = NSData(contentsOfURL: url),
json = NSJSONSerialization.JSONObjectWithData(
data, options: NSJSONReadingOptions(0),
error: nil) as? NSDictionary,
resultsList = json["results"] as? NSArray,
results = resultsList.firstObject as? NSDictionary,
@erica
erica / gist:05f9468573bf5ec8d98b
Created June 4, 2015 23:06
Drop Window for Playground
import Foundation
import Cocoa
import XCPlayground
public let DropNotification = "DropNotification"
public class DropWindow : NSWindow {
required public init?(coder: NSCoder) {
super.init(coder: coder)
self.registerForDraggedTypes([NSFilenamesPboardType])