Skip to content

Instantly share code, notes, and snippets.


Alejandro Ramirez janodev

View GitHub Profile
mugbug / AssertEquals+Diff.swift
Created Jun 10, 2021
XCTestCase wrapper for asserting equatable structs with formatted error diff message
View AssertEquals+Diff.swift
import XCTest
// Credits:
//swiftlint:disable empty_string force_cast force_unwrapping unused_closure_parameter function_body_length
class MyCustomTestCase: XCTestCase {
func assertEqual<T: Equatable>(
expected: T,
actual: T
) {
samrayner / Realm+CascadeDeleting.swift
Last active Jun 24, 2021 — forked from krodak/Realm+CascadeDeleting.swift
Cascading deletion for RealmSwift
View Realm+CascadeDeleting.swift
import Foundation
import RealmSwift
import Realm
//Forked from:
internal protocol CascadingDeletable: RealmSwift.Object {
static var propertiesToCascadeDelete: [String] { get }
fuji246 /
Last active Nov 9, 2019
UIPinchGestureRecognizer Scale

I find the scale of UIPinchGestureRecognizer is confusing, so I did two experiments below, two ways to do the same thing.

Method 1

gestureRecognizer.scale start with 1.0 at the beginning of pinch (gestureRecognizer.state == .began), and gestureRecognizer.scale in later state (.changed or .end) is always based on that, for example, if the view size is view_size at the beginning of pinch (might not be the same with the original size orig_view_size), gestureRecognizer.scale always starts with 1.0, and if it becomes 2.0 later, it's size will be 2 * view_size, so the scale always based on that when the pinch starts. And we can get the scale at the beginning of pinch (gestureRecognizer.state == .began) lastScale = self.imageView.frame.width/self.imageView.bounds.size.width, so the scale of the original image now should be lastScale * gestureRecognizer.scale

  • lastScale: The scale of last round of Pinch, a round of Pinch is from state.start to state.end, and the scale is based on the o
JohnCoates / UIGestureRecognizer+Closure.swift
Created May 18, 2017
UIGestureRecognizer with closure, Swift 3
View UIGestureRecognizer+Closure.swift
import UIKit
extension UIGestureRecognizer {
@discardableResult convenience init(addToView targetView: UIView,
closure: @escaping () -> Void) {
GestureTarget.add(gesture: self,
closure: closure,
toView: targetView)
ilyapuchka /
Last active Jul 15, 2019
Run swiftlint only on files changed from the latest commit
IFS=$(echo -en "\n\b")
FILES=($(git ls-files -m | grep ".*\.swift$" | grep -v ".*R.generated.swift$"))
if [[ ${FILES[@]} ]]; then
for i in "${!FILES[@]}"; do
export "SCRIPT_INPUT_FILE_$i"="${FILES[$i]}"
benjaminsnorris / TextEditing.swift
Last active Oct 4, 2020
Live reload of text in UITextView while preserving cursor position and text selection
View TextEditing.swift
import UIKit
class TextEditing: UIViewController {
@IBOutlet weak var textView: UITextView?
func updateText(with newString: String?) {
guard let textView = textView, newString = newString, (diffRange, changedText) = diff(textView.text, newString) else { return }
guard let selectedRange = textView.selectedTextRange else { textView.text = newString; return }
textView.text = newString
steipete /
Last active Apr 23, 2022
Using iOS 15 devices with Xcode 12.5 (instead of Xcode 13)
# The trick is to link the DeviceSupport folder from the beta to the stable version.
# sudo needed if you run the Mac App Store version. Always download the dmg instead... you'll thank me later :)
# Support iOS 15 devices (Xcode 13.0) with Xcode 12.5:
sudo ln -s /Applications/ /Applications/
# Then restart Xcode and reconnect your devices. You will need to do that for every beta of future iOS versions
# (A similar approach works for older versions too, just change the version number after DeviceSupport)
View ioslocaleidentifiers.csv
localeIdentifier Description
eu Basque
hr_BA Croatian (Bosnia & Herzegovina)
en_CM English (Cameroon)
rw_RW Kinyarwanda (Rwanda)
en_SZ English (Swaziland)
tk_Latn Turkmen (Latin)
he_IL Hebrew (Israel)
ar Arabic
uz_Arab Uzbek (Arabic)
aras-p / preprocessor_fun.h
Last active May 12, 2022
Things to commit just before leaving your job
View preprocessor_fun.h
// Just before switching jobs:
// Add one of these.
// Preferably into the same commit where you do a large merge.
// This started as a tweet with a joke of "C++ pro-tip: #define private public",
// and then it quickly escalated into more and more evil suggestions.
// I've tried to capture interesting suggestions here.
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_,
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant,