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 | |
protocol AutoEquatableEnum { | |
static func == (lhs: Self, rhs: Self) -> Bool | |
} | |
extension AutoEquatableEnum { | |
static func == (lhs: Self, rhs: Self) -> Bool { | |
return lhs.data == rhs.data | |
} |
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
// | |
// BlockBasedSelector.h | |
// | |
// Created by Charlton Provatas on 11/2/17. | |
// Copyright © 2017 CharltonProvatas. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface BlockBasedSelector : NSObject |
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
let callback: CGEventTapCallBack = { (tapProxy, eventType, event, refcon) -> Unmanaged<CGEvent>? in | |
debugPrint("we are monitoring the mouse event here") | |
return nil | |
} | |
let eventMask = (1 << CGEventType.leftMouseDown.rawValue) | (1 << CGEventType.leftMouseUp.rawValue) | |
let machPort = CGEvent.tapCreateForPid(pid: 40529, place: .tailAppendEventTap, options: .defaultTap, eventsOfInterest: CGEventMask(eventMask), callback: callback, userInfo: nil)! | |
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, machPort, 0) |
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
protocol TopLevelCollection: Codable { | |
associatedtype ElementType: Codable | |
var elements: [ElementType] { get set } | |
init(elements: [ElementType]) | |
} | |
extension TopLevelCollection { | |
public init(from decoder: Decoder) throws { | |
var container = try decoder.unkeyedContainer() |
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
extension Date { | |
var roundedToSecond: Date { | |
let date = self | |
let diff = 1000000000 - Calendar.current.component(.nanosecond, from: date) | |
return Calendar.current.date(byAdding: .nanosecond, value: diff, to: date)! | |
} | |
} | |
/// USAGE: | |
let roundedDate = Date().roundedToSecond /// zero nanoseconds |
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
//Uses only O(n) complexity. | |
extension Dictionary where Key == String, Value == Any? { | |
var trimmingNullValues: [String: Any] { | |
var copy = self | |
forEach { (key, value) in | |
if value == nil { | |
copy.removeValue(forKey: key) | |
} |
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
/// throttle a procedure for the given time interval, operation identifer is used to uniquely identify operation to prevent from firing multiple times | |
private var throttledOperations: Set<String> = [] | |
func dispatchThrottle(_ interval: TimeInterval, operationIdentifier: String, queue: DispatchQueue = .main, _ closure: @escaping () -> Void) { | |
guard !throttledOperations.contains(operationIdentifier) else { return } | |
throttledOperations.insert(operationIdentifier) | |
closure() | |
queue.asyncAfter(deadline: .now() + interval) { | |
throttledOperations.remove(operationIdentifier) | |
} | |
} |
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 | |
extension Data { | |
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription | |
guard let object = try? JSONSerialization.jsonObject(with: self, options: []), | |
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]), | |
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil } | |
return prettyPrintedString | |
} |
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 | |
extension CGImage { | |
var isBlank: Bool { | |
guard let data = dataProvider?.data, let buffer = CFDataGetBytePtr(data) else { return false } | |
let length = CFDataGetLength(data) | |
var i = 0 | |
while i < length { | |
if buffer[i] < 255, buffer[i + 1] < 255, buffer[i + 2] < 255 { | |
return false |
OlderNewer