Skip to content

Instantly share code, notes, and snippets.

@staticVoidMan
staticVoidMan / URLImage.swift
Created August 18, 2021 07:52
SwiftUI | Load image from url
/*
Refs:
- https://blog.devgenius.io/async-image-loading-its-combine-way-be203eae12f7
- https://www.fivestars.blog/articles/design-system-composing-views
- https://sarunw.com/posts/how-to-initialize-stateobject-with-parameters-in-swiftui
- https://www.simpleswiftguide.com/how-to-use-sf-symbols-in-swiftui
*/
//--- VIEW MODEL
import Combine
@staticVoidMan
staticVoidMan / DispatchDebouncer.swift
Last active July 26, 2021 12:39
Simple custom debouncer concept using DispatchWorkItem
class Debouncer {
private var task: DispatchWorkItem?
func perform(after interval: TimeInterval = 0.5, handler: @escaping ()->Void) {
cancel()
let task = DispatchWorkItem(block: handler)
DispatchQueue.global().asyncAfter(deadline: .now() + interval, execute: task)
self.task = task
@staticVoidMan
staticVoidMan / StringExtension.swift
Created June 28, 2021 14:07
Random Sequence of Letters
extension String {
static func randomLetters(ranged range: ClosedRange<Int>) -> String {
var randomCharacter: Character {
let range: ClosedRange<UInt16> = (97...122)
let unicode: Unicode.Scalar = .init(range.randomElement()!)!
let character = Character(unicode)
return character
}
let upperBound = range.randomElement()!
@staticVoidMan
staticVoidMan / link.swift
Created June 10, 2021 06:10
Find link in a string
@staticVoidMan
staticVoidMan / ArrayExtension+Sorter.swift
Last active May 4, 2021 12:21
Array extension providing support for specifying a Custom Sort implementation with live updates of swapped indices
//---------------------
//SORTER IMPLEMENTATION:
protocol Sorter {
typealias OrderingHandler<Element> = (Element, Element) throws -> Bool
func sort<Element>(_ array: inout [Element], by areInIncreasingOrder: OrderingHandler<Element>) rethrows
}
extension Array {
mutating func sort(using sorter: Sorter, by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows {
//Ref: https://www.youtube.com/watch?v=o4bAoo_gFBU
var array = [16,14,5,6,8]
for i in 0..<array.count - 1 {
var didSwap = false
for j in 0..<array.count - 1 - i {
let (firstIndex, secondIndex) = (j, j + 1)
if array[firstIndex] > array[secondIndex] {
ssh-keygen -t ed25519 -C "<YOUR_EMAIL_ADDRESS>"
git clone <SSH_LINK>
//if above did not work then check which ssh filename is being used with github:
ssh -vT git@github.com
ssh-add -K <PATH OF SSH PRIVATE KEY>
@staticVoidMan
staticVoidMan / todoWarnings
Created April 22, 2021 09:21
Show warnings in Xcode scipt for TODO & FIXME comments
Ref: https://medium.com/@cboynton/todo-make-your-notes-on-xcode-stand-out-5f5124ec064c
TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$|($ERRORTAG).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/" | perl -p -e "s/($ERRORTAG)/ error: \$1/"
/*
Ref:
- https://developer.apple.com/documentation/uikit/view_controllers/creating_a_custom_container_view_controller
- https://www.swiftbysundell.com/basics/child-view-controllers
*/
extension UIViewController {
func embed(child: UIViewController, in container: UIView) {
guard container.subviews.contains(child.view) == false else { return }
@staticVoidMan
staticVoidMan / shortcuts
Last active April 22, 2021 08:45
Assorted Terminal Shortcuts
xcrun simctl list
xcrun simctl erase <device UDID>
xcrun simctl erase all
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install