Skip to content

Instantly share code, notes, and snippets.

View kristofk's full-sized avatar

Kristof Kocsis kristofk

View GitHub Profile
@kristofk
kristofk / gitrenamebranch.sh
Last active July 26, 2019 06:21
Renames the current branch and locally and on remote. (gitrenamebransh.sh <remote_name> <old_branch_name> <new_branch_name>)
#!/bin/sh
# Calling this script:
# gitrenamebransh.sh <remote_name> <old_branch_name> <new_branch_name>
# Rename the local branch to the new name
git branch -m $2 $3
# Delete the old branch on remote - where <remote> is, for example, origin
git push $1 --delete $2
@kristofk
kristofk / TextField+shouldChangeCharactersIn.swift
Last active January 18, 2023 12:27
Custom reimplementation of textField(_:shouldChangeCharactersIn:replacamentString:)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentText = textField.text! as NSString
let newText = currentText.replacingCharacters(in: range, with: string)
textField.text = newText
return false
}
@kristofk
kristofk / Observable.swift
Last active August 17, 2019 10:12
Observer Pattern in Swift - Or at least my custom implementation
struct Observable<T> {
typealias ChangeHandler = (_ oldValue: T, _ newValue: T) -> Void
var value: T {
didSet {
notifySubscribers(oldValue: oldValue, newValue: value)
}
}
internal init(_ value: T) {
@kristofk
kristofk / Main.swift
Created January 21, 2021 21:23
Simple implementation of Knuth-Morris-Pratt (KMP) algorithm in Swift
// .─────────────────────.
// _.───' `────.
// ,' `.
// ( init(next/1: ℕ[m]; P/1: ∑[m]) )
// '─. ,─'
// `────. _.───'
// `──────────┬────────'
// │
// ┌─────────────────────────┴──────────────────────┐
// │ next[1] := i := 0; j := 1 │
import UIKit
extension UIAccessibilityTraits: @retroactive CustomStringConvertible {
public var description: String {
var traits = [String]()
if contains(.none) { traits.append("none") }
if contains(.button) { traits.append("button") }
if contains(.link) { traits.append("link") }
if #available(iOS 6.0, *) {
if contains(.header) { traits.append("header") }
@kristofk
kristofk / AccessibilityPropertyOverrides.swift
Created October 15, 2025 11:33
Overwrite UIAccessibility properties for easier control and debugging
var _accessibilityLabel: String? = ""
override var accessibilityLabel: String? {
get {
_accessibilityLabel
}
set {
print("[x] accessibilityValue: [\(_accessibilityLabel ?? "nil")] => [\(newValue ?? "nil")]")
_accessibilityLabel = newValue
}
}
@kristofk
kristofk / VoiceOverSeparatorLocals.swift
Last active December 2, 2025 08:39
Logic in Swift to print all the separators that different locales use. English and most languages use the standard ASCII comma to separate lists while some languages use sth else.
import Foundation
for locale in Locale.availableIdentifiers {
let list = ["1", "2", "3", "4"]
let listFormatter = ListFormatter()
listFormatter.locale = Locale(identifier: locale)
guard let formattedList = listFormatter.string(from: list) else {
print("\(locale) Failed to format.")
continue
}