Skip to content

Instantly share code, notes, and snippets.

@jamesujeon
jamesujeon / UIView+AncestorView.swift
Last active July 20, 2022 14:09
Get the ancestor view of the specific type
import UIKit
extension UIView {
func ancestorView<T: UIView>() -> T? {
var view = superview
while view != nil && !(view is T) {
view = view?.superview
}
return view as? T
@jamesujeon
jamesujeon / Collection+SafeSubscript.swift
Last active November 4, 2021 11:14
Subscript `Collection` safely with `Optional`
import Foundation
extension Collection {
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
subscript(safe range: Range<Index>) -> SubSequence? {
indices.contains(range.lowerBound) && indices.contains(range.upperBound) ? self[range] : nil
}
@jamesujeon
jamesujeon / String+SafeSubscript.swift
Last active November 4, 2021 11:17
Subscript `String` safely with `Optional`
import Foundation
extension String {
subscript(safe range: String.IndexDistance) -> Character? {
guard let index = index(at: range) else { return nil }
return self[index]
}
subscript(safe range: NSRange) -> Substring? {
@jamesujeon
jamesujeon / String+Subscript.swift
Last active November 4, 2021 11:17
Subscript `String` using the range expression
import Foundation
extension String {
subscript(range: String.IndexDistance) -> Character {
self[index(at: range)]
}
subscript(range: NSRange) -> Substring {
self[range.lowerBound..<range.upperBound]
}
@jamesujeon
jamesujeon / IDETemplateMacros.plist
Last active July 13, 2021 08:24
Put the simple file header template in 'YourProject.xcodeproj/xcshareddata'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FILEHEADER</key>
<string>
// ___FILENAME___
// ___PROJECTNAME___
//
// Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
@jamesujeon
jamesujeon / AppDelegate+UserInterfaceStyle.swift
Last active November 4, 2021 11:14
Change user interface style by inverting it
import UIKit
extension AppDelegate {
func changeUserInterfaceStyle() {
guard let window = window else { return }
if window.overrideUserInterfaceStyle == .unspecified {
window.overrideUserInterfaceStyle = UIScreen.main.traitCollection.userInterfaceStyle == .light ? .dark : .light
} else {
window.overrideUserInterfaceStyle = window.overrideUserInterfaceStyle == .light ? .dark : .light
@jamesujeon
jamesujeon / Reusable.swift
Last active October 20, 2021 01:37
Use class name for reuse identifier of something
import Foundation
protocol Reusable {
static var reuseIdentifier: String { get }
}
extension Reusable {
static var reuseIdentifier: String {
String(describing: self)
}
@jamesujeon
jamesujeon / HasCancellables.swift
Created July 20, 2022 14:21
Provide the `cancellables` property of `Set<AnyCancellable>` which helps you cancel subscriptions easily
import Foundation
import Combine
protocol HasCancellables: AnyObject {
var cancellables: Set<AnyCancellable> { get set }
}
extension HasCancellables {
var cancellables: Set<AnyCancellable> {
get {