Skip to content

Instantly share code, notes, and snippets.

View imjhk03's full-sized avatar
🪴
Gardening

Joohee Kim imjhk03

🪴
Gardening
View GitHub Profile
@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
@imjhk03
imjhk03 / Protocol Extensions.swift
Created July 29, 2021 05:49
Sample code about providing default value with protocol extensions
import UIKit
protocol Moveable {
func move(to point: CGPoint)
}
extension Moveable {
func move(to point: CGPoint = .zero) {
move(to: point)
}
@imjhk03
imjhk03 / scrollbar.swift
Last active May 25, 2021 06:38
Shows scrollbar over header or footer view (iOS 12 and below)
func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
// works from iOS 12 and below
view.layer.zPosition = 0
}
@imjhk03
imjhk03 / UIHelper+Zeplin.swift
Created January 9, 2021 07:26
A function to convert Zeplin line height to iOS line spacing
import UIKit
enum UIHelper {
/// Convert Zeplin line height to iOS line spacing
static func lineSpacing(from lineHeight: CGFloat, font: UIFont) -> CGFloat {
let value = lineHeight - font.lineHeight
return value
}
@imjhk03
imjhk03 / String+Date.swift
Created March 25, 2021 02:58
Useful String and Date extensions
import Foundation
extension String {
func toDate(format: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: self)
}
}