Skip to content

Instantly share code, notes, and snippets.

@daehn
daehn / SystemLog.swift
Created February 24, 2016 19:10 — forked from kristopherjohnson/SystemLog.swift
Demo of using the Apple System Log (ASL) API from Swift
// Note: This must be used in an Xcode project that contains a bridging header
// that includes <asl.h>
import Foundation
/// Provides high-level methods to access the raw data in
/// an ASL message.
struct SystemLogEntry {
/// Key-value pairs read from ASL message
let data: [String : String]
import Foundation
extension Array where Element : IntegerType {
var sum: Element {
return reduce(0, combine: +)
}
var odds: [Element] {
return filter { $0 % 2 != 0 }
}
@daehn
daehn / Permutations.swift
Created September 27, 2015 16:36
Swift Array extension to create interleaved arrays and permutations.
extension Array {
var decompose: (head: Element, tail: [Element])? {
return count > 0 ? (self[0], Array(self[1..<count])) : nil
}
}
extension Array {
func interleave(x: Element) -> [[Element]] {
guard let (head, tail) = decompose else { return [[x]] }
let start = [x] + self
@daehn
daehn / CountElement.Swift
Last active January 30, 2016 19:50
Extension on SequenceType to count the occurrence of an element.
extension SequenceType where Generator.Element : Equatable {
func count(other: Generator.Element) -> Int {
var sum = 0
for case other in self { sum++ }
return sum
}
}
func ~=<T>(pattern: T -> Bool, value: T) -> Bool {
return pattern(value)
@daehn
daehn / Searchable.swift
Last active April 23, 2019 03:05
Swift protocols and protocol extensions to easily add and delete conforming instances to Spotlight. Blog post can be found here: http://silvandaehn.com/2015/09/25/Simplifying-CoreSpotlight/
import UIKit
import CoreSpotlight
import MobileCoreServices
public typealias Completion = NSError? -> Void
// MARK: - Searchable Protocol
public protocol Searchable {
static var spotlightDomainIdentifier: String { get }
@daehn
daehn / script.swift
Last active September 18, 2015 17:41 — forked from zats/script.swift
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
extension SequenceType {
func first(@noescape predicate: Generator.Element -> Bool) -> Generator.Element? {
for element in self {
if predicate(element) {
return element
}
}
return nil
}
}
enum Beat {
case NoBeat, BPM(Int), Other(Int)
var value : Int? {
return Mirror(reflecting: self).children.first?.value as? Int
}
}
let enums: [Beat] = [.NoBeat, .BPM(105), .Other(120)]
enums.forEach { print($0.value) }
@daehn
daehn / Levenshtein.swift
Last active August 28, 2017 02:19
Calculate the Levenshtein-Distance between two Strings in Swift
extension String {
subscript(index: Int) -> Character {
return self[startIndex.advancedBy(index)]
}
subscript(range: Range<Int>) -> String {
let start = startIndex.advancedBy(range.startIndex)
let end = startIndex.advancedBy(range.endIndex)
@daehn
daehn / CDHelper.swift
Last active August 31, 2015 15:20
Small NSManagedObject extension to simplify fetching and creating objects.
import Foundation
import CoreData
extension NSManagedObject {
class var entityName: String {
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
convenience init(context: NSManagedObjectContext) {