Skip to content

Instantly share code, notes, and snippets.

View yhkaplan's full-sized avatar
⚙️

Joshua Kaplan yhkaplan

⚙️
View GitHub Profile
@yhkaplan
yhkaplan / ImageDownsampler.swift
Created February 25, 2022 05:37
Downsample images to reduce memory impact
enum ImageDownsampler {
private static let pixelSize = 2_000
/// - URL: https://christianselig.com/2020/09/phpickerviewcontroller-efficiently/
static func downsample(imageAt url: URL) -> Data? {
let sourceOptions: [CFString: Any] = [kCGImageSourceShouldCache: false]
let downsampleOptions: [CFString: Any] = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: pixelSize
@yhkaplan
yhkaplan / IdentifiableItem.swift
Created November 5, 2021 05:52
Convenient wrapper to make existing types conform to identifiable without changing their definition
let a = IdentifiableItem(User(id: 1))
let b = IdentifiableItem(User(id: 1), id: \.id)
@dynamicMemberLookup
struct IdentifiableItem<Item, ID: Hashable>: Identifiable {
var item: Item
let id: ID
init(_ item: Item) where ID == UUID {
self.item = item
#!/usr/bin/env sh
extract_name() {
echo "$1" | sed -rn 's/.*\/(.*)\.colorset.*/\1/p'
}
extract_hex() {
FILE_CONTENTS=$(cat "$1")
COMPONENTS=$(echo "$FILE_CONTENTS" | jq '.colors[0].color.components')
@yhkaplan
yhkaplan / delete_xcode_breakpoints.scpt
Created August 4, 2021 09:03
Selects and deletes all Xcode breakpoints. Makes for a useful checkout-hook
tell application "Xcode" to activate
tell application "System Events"
keystroke "8" using command down -- Show all breakpoints
keystroke "a" using command down -- Select all breakpoints
keystroke (ASCII character 127) -- Delete with backspace key
end tell
@yhkaplan
yhkaplan / Podfile.rb
Created July 27, 2020 09:03
Make cocoapods static
pre_install do |installer|
installer.pod_targets.each do |pod|
if !dynamic_frameworks.include?(pod.name)
puts "Overriding the static_framework? method for #{pod.name}"
def pod.static_framework?;
true
end
end
end
end
#!/usr/bin/env sh
PLIST_BUDDY="/usr/libexec/PlistBuddy"
INFO_PLIST_PATH="app/Info.plist"
CURRENT_FULL_VERSION=$("$PLIST_BUDDY" -c "Print CFBundleVersion" "$INFO_PLIST_PATH")
CURRENT_MARKETING_VERSION=$("$PLIST_BUDDY" -c "Print CFBundleShortVersionString" "$INFO_PLIST_PATH")
ALL_INFO_PLIST_FILES=$(git grep --files-with-matches "CFBundleVersion" -- "*Info.plist")
for INFO_PLIST_FILE in $ALL_INFO_PLIST_FILES; do
infix operator ???
extension Optional where Wrapped == String {
var isEmptyOrNil: Bool {
return self == nil || self == ""
}
static func ???(lhs: String?, rhs: String) -> String {
guard let lhs = lhs, lhs != "" else {
return rhs
// Swift safety
/// Type safety
let i: UInt8 = 1
let x: Int = 4
// Error
// let result = i*x
@yhkaplan
yhkaplan / URLSession+Codable.swift
Created January 15, 2019 06:03
Basic explainer resource for beginners
import Foundation
import PlaygroundSupport
// Model
struct Character: Decodable {
let name: String
}
// NW
@yhkaplan
yhkaplan / RegexExtensions.swift
Last active January 15, 2019 07:20
Swifty wrapper for NSRegularExpression
import Foundation
struct Regex: ExpressibleByStringLiteral {
private let pattern: String
private var nsRegularExpression: NSRegularExpression? {
return try? NSRegularExpression(pattern: pattern)
}
typealias StringLiteralType = String