Skip to content

Instantly share code, notes, and snippets.

let fname = "/tmp/scratch.txt"
let fptr = fopen(fname, "w")
if (fptr == nil) {
let localErrno = errno
println("Unable to open \(fname): \(strerror(localErrno))")
} else {
let text = "Hello World\n"
let bytesWritten = fwrite(text, UInt(sizeof(Byte)), strlen(text), fptr)
println("Wrote \(bytesWritten) bytes")
fclose(fptr)
@erica
erica / gist:e4b3fca1eba67536e4a3
Last active August 29, 2015 14:04
Which one?
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 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
// Thanks, Lily Ballard
extension Optional {
func and<U>(x:U?) -> (T, U)? {
switch (self, x) {
case let (.Some(first), .Some(second)):
return (first, second)
default:
return nil
}
}
@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:ea2ea1b3cc30a9ba952c
Last active December 11, 2018 01:03
Tuple assignment
//
// TupleAssignment.swift
// Hello Swift
//
// Created by Erica Sadun on 12/19/14.
// Copyright (c) 2014 Erica Sadun. All rights reserved.
//
import Foundation
@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