Skip to content

Instantly share code, notes, and snippets.

Erica Sadun erica

Block or report user

Report or block erica

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:e4b3fca1eba67536e4a3
func ErrorString1(error : NSError?) -> String {
if let theError = error as? NSError {
if let description = theError.localizedDescription {
return description
}
}
return ""
}
func ErrorString2(error_ : NSError?) -> String {
@erica
erica / Euler Challenge
Created Jul 30, 2014
Explore Swift Challenge #1
View Euler Challenge
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
View gist:88ceabc67b4d1607c251
// 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) {
View gist:e7b358dcf7da1de9b34e
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 []
}
View gist:85b74f25b93f9e54843c
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
View gist:c058d1d10fd0f1f04c91
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 Aug 29, 2015
Using a postfix operator to bridge to objective c
View gist:e2fbd5c910311d1c96d2
postfix operator ⟹ {}
postfix func ⟹ <T:_ObjectiveCBridgeable>(obj : T) -> AnyObject { return _bridgeToObjectiveC(obj)! }
var s = "23.692"
NSString(string:s).doubleValue
s⟹.doubleValue
View gist:f3aad9e491df716725d3
// 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 Mar 29, 2015
Before and After
View Last night
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 Aug 29, 2015
If let tuples in action
View gist:da7560c2acb80e7617ad
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:
You can’t perform that action at this time.