Skip to content

Instantly share code, notes, and snippets.

View imjhk03's full-sized avatar
🪴
Gardening

Joohee Kim imjhk03

🪴
Gardening
View GitHub Profile
extension StringProtocol {
subscript(_ offset: Int) -> String.Element {
if offset >= 0 {
self[index(startIndex, offsetBy: offset)]
} else {
self[index(endIndex, offsetBy: offset)]
}
}
@imjhk03
imjhk03 / AdventOfCode+Plus.swift
Last active December 11, 2024 12:13
Few examples from Swift and the Swift Algorithms core package
import Algorithms
// The ~= operator means "contains", and is used to test if a value is within a range.
let seq = 9...13
print(seq ~= 9) // true
print(seq ~= 50) // false
// The .adjacentPairs() method lets you iterate through a sequence in pairs of current + next.
let pairs = ["a", "b", "c", "d"].adjacentPairs()
for pair in pairs { print(pair) } // ("a", "b"), ("b", "c"), ("c", "d")
@imjhk03
imjhk03 / String+Extensions.swift
Last active September 12, 2024 09:02
Swift String Cheat Sheet
import Foundation
/// Find specific character in String using String.Index (Unicode-compliant way)
/// Usage:
/// let word = "Banana"
/// let fifthCharacter = word[4]
/// print("The 5th character is: \(fifthCharacter)") // Prints: The 5th character is: n
extension StringProtocol {
subscript(offset: Int) -> Character {
self[index(startIndex, offsetBy: offset)]
@imjhk03
imjhk03 / Git commands
Last active May 16, 2024 14:34
Git Cheat Sheet
// Changing the most recent commit message
1. Run 'git commit --amend' in terminal
2. Edit the commit message
3. Save and close the opened default text editor
@imjhk03
imjhk03 / Array.swift
Last active April 30, 2024 09:18
Swift Array Cheat Sheet
// Initialize an array with fixed number of default values
var digitCounts = Array(repeating: 0, count: 5)
// [0, 0, 0, 0, 0]
// Convert an array of integers to an array of strings, use 'map()'
let numbers: [Int] = [1, 2, 3, 4, 5]
let stringArray = numbers.map { String($0) }
// Find the maximum element in an array, can be nil
let numbers = [1, 2, 3, 4, 5]
@imjhk03
imjhk03 / SwiftUI+UIKit.swift
Created September 14, 2023 06:57
Make SwiftUI view to UIView
extension View {
func hostingView() -> UIView {
let hostingView = UIHostingController(rootView: self)
return hostingView.view
}
}
@imjhk03
imjhk03 / OpenURL.swift
Last active September 12, 2023 09:23
Open a URL in Safari or App (Helper)
enum OpenURL {
enum Scheme {
case url(String)
case tel(String)
case app(String)
}
static func open(scheme: OpenURL.Scheme, completion: ((Bool) -> Void)? = nil) {
let application = UIApplication.shared
let url: URL
@imjhk03
imjhk03 / SwipeBackGestureViewController.swift
Created May 5, 2023 06:48
Enable swipe back when navigation bar is hidden
class SwipeBackGestureViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.hidesBarsOnSwipe = true
navigationController?.interactivePopGestureRecognizer?.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
@imjhk03
imjhk03 / MarkupFormatting.swift
Created January 8, 2023 07:41
Swift Playground Markup Formatting Reference
1. Using Markup
Single Line comment
//: line of markup content
Multi Line Comment
/*:
...
*/
2. Headings
@imjhk03
imjhk03 / PizzaBuilder.swift
Last active September 22, 2021 16:58
Sample code for builder pattern in swift.
import Foundation
enum Dough: String {
case basic
case cheeseCrust
}
enum Topping: String, CaseIterable {
case pepperoni
case sausage