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
#!/usr/bin/env xcrun swift | |
import Foundation | |
func Fortune() -> String? { | |
if let urlString = "http://iheartquotes.com/api/v1/random?format=json".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding), | |
url = NSURL(string:urlString), | |
data = NSData(contentsOfURL: url), | |
json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil) as? NSDictionary, | |
quote = json["quote"] as? 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
// My evil | |
extension Range { | |
var NSRangeValue : NSRange { | |
let location = reflect(startIndex).summary.toInt() ?? 0 | |
let length = reflect(distance(startIndex, endIndex)).summary.toInt() ?? 0 | |
return NSMakeRange(location, length) | |
} | |
} | |
let testString = "abcdefghij" |
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 UIKit | |
typealias Tupletype = (a:Int, b:String, c:Double) | |
let params1 = (1, "Params1", 2.4) // anonymous tuple | |
let params2 : Tupletype = (1, "Params2", 2.4) // named parameter tuple | |
// Standard tuple: var myclosure: (Int, String, Double) -> () | |
var myclosure = {(a:Int, b:String, c:Double)->() in println("\(a) \(b) \(c)")} | |
myclosure(params1) // works | |
// myclosure(params2) // does not work |
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 ToArray<S : SequenceType>(sequence: S) -> Array<S.Generator.Element> { | |
return Array<S.Generator.Element>(sequence) | |
} |
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 | |
public struct RandomGenerator<C: CollectionType> : GeneratorType, SequenceType { | |
private var backingGenerator : PermutationGenerator<C, [C.Index]> | |
public init(_ elements : C) { | |
var indices = Array(elements.startIndex..<elements.endIndex) | |
for index in 0..<count(indices) { | |
var swapIndex = index + Int(arc4random_uniform(UInt32(count(indices) - index))) | |
if swapIndex != index { | |
swap(&indices[index], &indices[swapIndex]) |
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
public let diagnosticsPath = (NSProcessInfo.processInfo().environment["DYLD_INSERT_LIBRARIES"] as! String).stringByDeletingLastPathComponent.stringByDeletingLastPathComponent.stringByAppendingPathComponent("AuxiliaryDiag.map") |
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 UIKit | |
import XCPlayground | |
// Please enable simulator to view results in playground | |
extension UIView { | |
class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) { | |
UIView.beginAnimations(nil, context: nil) | |
UIView.setAnimationDuration(duration) | |
animations() |
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 UIKit | |
import XCPlayground | |
// Enable simulator to view results in playground | |
extension UIView { | |
class func animate(duration : NSTimeInterval, @autoclosure _ animations : () -> Void) { | |
UIView.beginAnimations(nil, context: nil) | |
UIView.setAnimationDuration(duration) | |
animations() |
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
/// Fetch item by class | |
public func SelectTypedItemFromArray<T, U: SequenceType>(type t: T.Type, array: U) -> T? { | |
for eachItem in array { | |
if let typedItem = eachItem as? T { | |
return typedItem | |
} | |
} | |
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
public func CreateConvolve(kernel : [Int16]) -> UIImage -> UIImage? { | |
return {(image : UIImage) -> UIImage? in | |
let data = DataFromInt16Kernel(kernel) | |
return Convolve(image, data)} | |
} |