This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ErrorString1(error : NSError?) -> String { | |
if let theError = error as? NSError { | |
if let description = theError.localizedDescription { | |
return description | |
} | |
} | |
return "" | |
} | |
func ErrorString2(error_ : NSError?) -> String { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TupleAssignment.swift | |
// Hello Swift | |
// | |
// Created by Erica Sadun on 12/19/14. | |
// Copyright (c) 2014 Erica Sadun. All rights reserved. | |
// | |
import Foundation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postfix operator ⟹ {} | |
postfix func ⟹ <T:_ObjectiveCBridgeable>(obj : T) -> AnyObject { return _bridgeToObjectiveC(obj)! } | |
var s = "23.692" | |
NSString(string:s).doubleValue | |
s⟹.doubleValue |