Skip to content

Instantly share code, notes, and snippets.

Avatar

Quinton Pryce quintonpryce

View GitHub Profile
@quintonpryce
quintonpryce / IntentPersonProvider.swift
Created Nov 2, 2020
Retrieving a contact's phone number from a name in an INPerson object
View IntentPersonProvider.swift
import Contacts
import Intents
protocol ContactStoring {
func enumerateContacts(with fetchRequest: CNContactFetchRequest, usingBlock block: @escaping (CNContact, UnsafeMutablePointer<ObjCBool>) -> Void) throws
}
extension CNContactStore: ContactStoring {}
class IntentPersonProvider {
@quintonpryce
quintonpryce / IntentHandler.swift
Last active Nov 2, 2020
IntentHandler for INStartCallHandling
View IntentHandler.swift
import Contacts
import Intents
class IntentHandler: INExtension, INStartCallIntentHandling {
/// CNContactsStore implementation to get a person from contacts.
///
/// See https://gist.github.com/quintonpryce/d69499bf40bdef208bd35d00aba188db
lazy var personProvider = IntentPersonProvider()
@available(iOSApplicationExtension 13.0, *)
@quintonpryce
quintonpryce / Levenshtein.swift
Created Dec 31, 2019 — forked from RuiCarneiro/Levenshtein.swift
Levenshtein distance between two String for Swift 4.x
View Levenshtein.swift
import Foundation
extension String {
subscript(index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}
extension String {
public func levenshtein(_ other: String) -> Int {
View AnalyticsCore.swift
open class Analytics: AnalyticsScope {
public static let shared: AnalyticsScope = Analytics()
private var eventTrackers: [EventTrackerKey: EventTracker] = [:]
private let failureHandler: FailureHandler
public init() {
failureHandler = SimpleFailureHandler()
}
View AnalyticsScope.swift
public protocol AnalyticsScope {
func track(event: AnalyticsEvent)
func track<T: AnalyticsParameter>(event: AnalyticsEvent, with parameters: T)
func addEventTracker(key: EventTrackerKey, tracker: EventTracker)
}
View ErrorHandling2.swift
private func trackAnalytic<T: AnalyticsParameter>(event: AnalyticsEvent, parameters: T?) {
if event.supportedTrackers.isEmpty {
failureHandler.failure("Event \(event.name) does not have any supported EventTrackers.")
return
}
for trackerKey in event.supportedTrackers {
if let eventTracker = eventTrackers[trackerKey] {
if eventTracker.isEventNameSupported(event: event.name) {
if let parameters = parameters, !parameters.isEmpty() {
View ErrorHandling1.swift
private func trackAnalytic<T: AnalyticsParameter>(event: AnalyticsEvent, parameters: T?) {
if event.supportedTrackers.isEmpty {
failureHandler.failure("Event \(event.name) does not have any supported EventTrackers.")
return
}
for trackerKey in event.supportedTrackers {
if let eventTracker = eventTrackers[trackerKey] {
if let parameters = parameters, !parameters.isEmpty() {
eventTracker.track(event: event.name, with: parameters)
View CoreInit.swift
private let failureHandler: FailureHandler
public init() {
failureHandler = SimpleFailureHandler()
}
public init(failureHandler: FailureHandler) {
self.failureHandler = failureHandler
}
View FailureHandling.swift
public protocol FailureHandler {
func failure(_ message: String)
}
class SimpleFailureHandler: FailureHandler {
func failure(_ message: String) {
assertionFailure(message)
}
}
View EmptyParameter.swift
class EmptyParameter: AnalyticsParameter {
var parameter: Bool?
typealias ParameterType = Bool?
init() {
parameter = nil
}
}