Skip to content

Instantly share code, notes, and snippets.

@ericdke
ericdke / weightFormatter.swift
Created March 12, 2016 15:04
NSNumberFormatter subclass to express weight down to the microgram
// http://stackoverflow.com/a/35959239/2227743
enum MassUnit: Double {
case Microgram = 1e-6
case Milligram = 1e-3
case Gram = 1
case Kilogram = 1e3
static let allUnits: [MassUnit] = [.Microgram, .Milligram, .Gram, .Kilogram]
@ericdke
ericdke / delegationExample02.swift
Created February 27, 2016 18:10
Delegation example #2 for the Aya.io tutorial.
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
protocol CanBuildHouses {
func startWorking()
}
protocol CanManageTeams {
@ericdke
ericdke / delegationExample01.swift
Created February 27, 2016 18:09
Delegation example #1 for the Aya.io tutorial.
protocol ListensToEvents {
func anEventHappened(code: Int)
}
class ClassicPrinter: ListensToEvents {
func anEventHappened(code: Int) {
print("CODE: \(code)")
}
}
@ericdke
ericdke / nilOr.swift
Created February 27, 2016 14:49
"assign value only if nil" operator like in Ruby
infix operator ||= {}
func ||=<T>(inout left: Optional<T>, right: T) { left = left ?? right }
var foo:String? = nil // foo -> nil
foo ||= "Hello" // foo -> "Hello"
foo ||= "Hi" // foo -> "Hello"
@ericdke
ericdke / blur.swift
Last active November 30, 2023 14:26
Swift: blur an UIImage with the Accelerate framework
import UIKit
import XCPlayground
import Accelerate
extension UIImage {
public func blur(size: Float) -> UIImage! {
let boxSize = size - (size % 2) + 1
let image = self.CGImage
let inProvider = CGImageGetDataProvider(image)
@ericdke
ericdke / permutations.swift
Created January 24, 2016 00:35
Swift: Heap's algorithm with Generics
public extension CollectionType {
var permutations: [[Generator.Element]] {
func permute<C: CollectionType>(items: C) -> [[C.Generator.Element]] {
var scratch = Array(items) // This is a scratch space for Heap's algorithm
var result: [[C.Generator.Element]] = [] // This will accumulate our result
// Heap's algorithm
func heap(n: Int) {
if n == 1 {
result.append(scratch)
return
let url = NSURL(fileURLWithPath: imagePath)
let cgiSrc = CGImageSourceCreateWithURL(url,nil)
let cfD:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(cgiSrc!, 0, nil)!
let nsDic = NSDictionary(dictionary: cfD)
print(nsDic.description)
@ericdke
ericdke / extractURLS.swift
Created January 21, 2016 18:38
SWIFT: Extract URLS from String
extension String {
func extractURLs() -> [NSURL] {
var urls : [NSURL] = []
do {
let detector = try NSDataDetector(types: NSTextCheckingType.Link.rawValue)
detector.enumerateMatchesInString(self,
options: [],
range: NSMakeRange(0, text.characters.count),
usingBlock: { (result, _, _) in
if let match = result, url = match.URL {
import AVFoundation
func getImageFromVideo(videoURL: String) -> UIImage? {
do {
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: videoURL), options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil)
let image = UIImage(CGImage: cgImage)
return image
let binNum = 42
let binStr = String(binNum, radix: 2)
print(binStr)
let octNum = 42
let octStr = String(octNum, radix: 8)
print(octStr)
let hexNum = 42
let hexStr = String(hexNum, radix: 16)