This file contains hidden or 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
| // 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] | |
This file contains hidden or 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 XCPlayground | |
| XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
| protocol CanBuildHouses { | |
| func startWorking() | |
| } | |
| protocol CanManageTeams { |
This file contains hidden or 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
| protocol ListensToEvents { | |
| func anEventHappened(code: Int) | |
| } | |
| class ClassicPrinter: ListensToEvents { | |
| func anEventHappened(code: Int) { | |
| print("CODE: \(code)") | |
| } | |
| } |
This file contains hidden or 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
| 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" |
This file contains hidden or 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 | |
| import Accelerate | |
| extension UIImage { | |
| public func blur(size: Float) -> UIImage! { | |
| let boxSize = size - (size % 2) + 1 | |
| let image = self.CGImage | |
| let inProvider = CGImageGetDataProvider(image) |
This file contains hidden or 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 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 |
This file contains hidden or 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 url = NSURL(fileURLWithPath: imagePath) | |
| let cgiSrc = CGImageSourceCreateWithURL(url,nil) | |
| let cfD:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(cgiSrc!, 0, nil)! | |
| let nsDic = NSDictionary(dictionary: cfD) | |
| print(nsDic.description) |
This file contains hidden or 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 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 { |
This file contains hidden or 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 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 |
This file contains hidden or 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 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) |