Skip to content

Instantly share code, notes, and snippets.

@cprovatas
cprovatas / Data+PrettyPrint.swift
Created May 23, 2018 15:52
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
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
}
@cprovatas
cprovatas / Array+Spread.swift
Last active July 22, 2023 02:04
Spread Syntax for Swift Arrays (ala es6)
extension Array {
static func ... (lhs: [Self.Element], rhs: [Self.Element]) -> [Self.Element] {
var copy = lhs
copy.append(contentsOf: rhs)
return copy
}
static func ... (lhs: Self.Element, rhs: [Self.Element]) -> [Self.Element] {
var copy = [lhs]
copy.append(contentsOf: rhs)
@cprovatas
cprovatas / BlockBasedSelector.h
Last active June 1, 2023 01:34
Block-Based Selectors in Swift
//
// BlockBasedSelector.h
//
// Created by Charlton Provatas on 11/2/17.
// Copyright © 2017 CharltonProvatas. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BlockBasedSelector : NSObject
@cprovatas
cprovatas / gist:8d4dafaa13da058da32b0f68f625b424
Created October 19, 2017 17:49
Solution to eliminate boilerplate code when Implementing Equatable for Associated Value Enums
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
}
@cprovatas
cprovatas / gist:6acef442fc43123bcd5d5e937dc7951a
Created January 6, 2018 21:57
Monitor Mouse Event for any arbitrary application on macOS in Swift
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)
@cprovatas
cprovatas / RoundedSecond.swift
Created February 21, 2018 19:06
Round Date to Second
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
@cprovatas
cprovatas / interpolation.swift
Created June 8, 2019 22:56
Decorating Command Line Output With DefaultStringInterpolation
enum ASCIIColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
@cprovatas
cprovatas / SequenceHelpers.swift
Last active October 12, 2019 07:22
Swift Sequence Helpers
// These helpers allow for basic operations to be performed while still iterating over a sequence once without
// adding all of the common boilerplate code you'd normally have to write
extension Sequence {
// filter + forEach
func forEach(where predicate: (Element) -> Bool, _ body: (Element) throws -> Void) rethrows {
for element in self where predicate(element) {
try body(element)
}
}
@cprovatas
cprovatas / TypeReflectable.swift
Last active October 20, 2018 22:28
Access current type by calling Self instead of manually calling <current type>.self
protocol TypeReflectable {
var `Self`: Self.Type { get }
static var `Self`: Self.Type { get }
}
extension TypeReflectable {
var `Self`: Self.Type {
return type(of: self)
}
@cprovatas
cprovatas / CGImage+Utilities.swift
Last active July 6, 2018 23:00
Determine if a CGImage is completely white
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