Skip to content

Instantly share code, notes, and snippets.

Avatar
👩‍💻
Coding for living and fun

Joohee Kim imjhk03

👩‍💻
Coding for living and fun
View GitHub Profile
@imjhk03
imjhk03 / PizzaBuilder.swift
Last active Sep 22, 2021
Sample code for builder pattern in swift.
View PizzaBuilder.swift
import Foundation
enum Dough: String {
case basic
case cheeseCrust
}
enum Topping: String, CaseIterable {
case pepperoni
case sausage
@imjhk03
imjhk03 / Protocol Extensions.swift
Created Jul 29, 2021
Sample code about providing default value with protocol extensions
View Protocol Extensions.swift
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
Shows scrollbar over header or footer view (iOS 12 and below)
View scrollbar.swift
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 / String+Date.swift
Created Mar 25, 2021
Useful String and Date extensions
View String+Date.swift
import Foundation
extension String {
func toDate(format: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: self)
}
}
@imjhk03
imjhk03 / filter-map-reduce.swift
Created Mar 1, 2021
A simple example of filter, map, reduce method
View filter-map-reduce.swift
/*
Filter
*/
let familyNames = ["Park", "Lee", "Jong", "Kim", "Choi"]
let longFamilyNames = familyNames.filter { $0.count > 3 }
print(longFamilyNames)
// ["Park", "Jong", "Choi"]
let familyNames = ["Park", "Lee", "Jong", "Kim", "Choi"]
@imjhk03
imjhk03 / UIHelper+Zeplin.swift
Created Jan 9, 2021
A function to convert Zeplin line height to iOS line spacing
View UIHelper+Zeplin.swift
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
}
View String+Extension.swift
extension String {
var isValidEmail: Bool {
let emailFormat = ""
let emailPredicate = NSPredicate(format: "SELF MATCHES %@", emailFormat)
return emailPredicate.evaluate(with: self)
}
var isValidPassword: Bool {
//Regex restricts to 8 character minimum, 1 capital letter, 1 lowercase letter, 1 number
View UIView+Subview.swift
import UIKit
extension UIView {
func addSubviews(_ views: UIView...) {
for view in views { addSubview(view) }
}
}
@imjhk03
imjhk03 / UIView+Layout.swift
Created Dec 13, 2020
Layout(anchor) views easily extending from UIView
View UIView+Layout.swift
import UIKit
struct AnchoredConstraints {
var top, leading, bottom, trailing, width, height: NSLayoutConstraint?
}
extension UIView {
@discardableResult
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
@imjhk03
imjhk03 / ScreenSize and DeviceTypes of iPhone
Last active Dec 13, 2020
Get screen size or device types of iPhone
View ScreenSize and DeviceTypes of iPhone
enum ScreenSize {
static let width = UIScreen.main.bounds.size.width
static let height = UIScreen.main.bounds.size.height
static let maxLength = max(ScreenSize.width, ScreenSize.height)
static let minLength = min(ScreenSize.width, ScreenSize.height)
}
enum DeviceTypes {
static let idiom = UIDevice.current.userInterfaceIdiom