Skip to content

Instantly share code, notes, and snippets.

@erica
erica / Euler Challenge
Created July 30, 2014 20:13
Explore Swift Challenge #1
struct PrimeGenerator : Generator {
typealias Element = Int
private var myPrimes = [Int]() // store previous primes
var current : Int = 2 // first prime is 2
mutating func next() -> Int? {
if myPrimes.count == 0 {myPrimes = [2]; return 2}
NextNumber: while (current++ > 0) {
for prime in myPrimes {
// If a number has a prime factor it is not prime
// This should have worked. It doesn't work.
// Out of range error because the String.Index is set with regard to the smallest string at match
//extension String {
//
// func findIndexOf(substring : String) -> String.Index? {
// if countElements(self) < countElements(substring) {return nil}
// if countElements(substring) == 0 {return nil}
// if self.hasPrefix(substring) {return self.startIndex}
// if let recursionResults = dropFirst(self).findIndexOf(substring) {
extension Array {
func takeWhile(processingClosure : Element->Bool) -> [Element] {
if (self.count == 0) {return []}
let car : Element = self.first!
if processingClosure(car) {
let cdr : [Element] = Array(self[self.startIndex.successor()..<self.endIndex])
return [car] + cdr.takeWhile(processingClosure)
} else {
return []
}
Testing iPhone 6 plus
iPhone Simulator
iPhone OS
8.0
iPhone Simulator
<UIView: 0x7f854596f7a0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x7f854596bd50>>
<UIWindow: 0x7f854596e1e0; frame = (0 0; 414 736); gestureRecognizers = <NSArray: 0x7f854596edd0>; layer = <UIWindowLayer: 0x7f854596c0d0>>
Testing iPhone 6
iPhone Simulator
@erica
erica / gist:c058d1d10fd0f1f04c91
Last active August 29, 2015 14:13
Reflection work
import Foundation
func QLString(x : QuickLookObject) -> String {
switch x {
case .Text(let s) : return "[Text] " + s
case .Int(let i) : return "[Int] " + "\(i)"
case .UInt(let u) : return "[UInt] " + "\(u)"
case .Float(let f) : return "[Float] " + "\(f)"
case .Image(let img) : return "[Image] " + "[image]"
case .Sound(let snd) : return "[Sound] " + "[sound]"
@erica
erica / gist:e2fbd5c910311d1c96d2
Last active August 29, 2015 14:13
Using a postfix operator to bridge to objective c
postfix operator ⟹ {}
postfix func ⟹ <T:_ObjectiveCBridgeable>(obj : T) -> AnyObject { return _bridgeToObjectiveC(obj)! }
var s = "23.692"
NSString(string:s).doubleValue
s⟹.doubleValue
@erica
erica / gist:f3aad9e491df716725d3
Created March 22, 2015 19:07
Mirror Utilities
// MARK: Mirror-based Utilities
// These work across arrays, structures, tuples
// Goatees are mandatory
func mirrorDo<S>(structure:S, closure:(Int, String, Any)->()) {
let mirror = reflect(structure)
for index in 0 ..< mirror.count {
closure(index, mirror[index].0, mirror[index].1.value)
}
}
@erica
erica / Last night
Created March 29, 2015 16:40
Before and After
public extension UIBezierPath {
public var dashPhase : CGFloat? {
get {
// Only defined if dashes are defined
if dashes.count == 0 {return nil}
var phase = UnsafeMutablePointer<CGFloat>.alloc(1)
getLineDash(nil, count: nil, phase: phase)
let result = phase.memory; free(phase)
return result
}
@erica
erica / gist:da7560c2acb80e7617ad
Last active August 29, 2015 14:18
If let tuples in action
var interpolatedPathPoints : [CGPoint] {
let numberOfBezierSamples = 6
var results = [CGPoint]()
var current : BezierElement? = nil
for element in elements {
switch element.elementType.value {
case kCGPathElementMoveToPoint.value:
if let point = element.point {results += [point]; current = element}
break;
case kCGPathElementAddLineToPoint.value:
@erica
erica / gist:7c298164409795bbf92f
Last active August 29, 2015 14:18
Launch at Login Helper
//
// LaunchAtLoginHelper.swift
//
// Created by Erica Sadun on 4/1/15.
// Copyright (c) 2015 Erica Sadun. All rights reserved.
//
import Foundation
public func getLoginItems() -> LSSharedFileList? {