Skip to content

Instantly share code, notes, and snippets.

View nearfri's full-sized avatar

Ukjeong Lee nearfri

  • Gyeonggi-do, Republic of Korea
View GitHub Profile
@nearfri
nearfri / FirstResponder.swift
Created January 17, 2024 01:56
Find first responder
extension UIApplication {
fileprivate static var _firstResponder: UIResponder?
public var firstResponder: UIResponder? {
Self._firstResponder = nil
sendAction(#selector(findFirstResponder), to: nil, from: nil, for: nil)
return Self._firstResponder
}
// ModelID or TypedID 🤔
public struct ModelID<Model, RawValue>: RawRepresentable {
public let rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
public init(_ rawValue: RawValue) {
self.init(rawValue: rawValue)
@propertyWrapper
@dynamicMemberLookup
public struct PlainBinding<Value> {
private let get: () -> Value
private let set: (Value) -> Void
public init(get: @escaping () -> Value, set: @escaping (Value) -> Void) {
self.get = get
self.set = set
}
enum Bash {
@discardableResult
static func execute(command: String, arguments: [String] = []) throws -> String {
let path = try execute(path: "/bin/bash", arguments: ["-lc", "which \(command)"])
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
return try execute(path: path, arguments: arguments)
}
@discardableResult
static func execute(path: String, arguments: [String] = []) throws -> String {
@nearfri
nearfri / CustomTypedNotification.swift
Last active June 29, 2024 10:08
Typed Notification
public protocol CustomTypedNotification: TypedNotification {}
extension CustomTypedNotification {
public static var name: Notification.Name {
return .init(String(reflecting: self))
}
public static func generate(from notification: Notification) -> Self {
let notificatonKey = TypedUserInfo.notificationUserInfoKey
@nearfri
nearfri / Channel.swift
Last active April 24, 2019 04:44
Event channel
import Foundation
// Inspired by https://medium.com/developermind/using-channels-for-data-flow-in-swift-14bbdf27b471
public class EventChannel<Value> {
private typealias SubscriptionID = ObjectIdentifier
private var subscriptions: [SubscriptionID: Subscription] = [:]
public init() {}
@nearfri
nearfri / NSObject+KVO.swift
Last active March 28, 2019 08:20
Convenient Key-Value Observing
import Foundation
// ref.: https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/NSObject.swift
public struct KVOChange<Value> {
public typealias Kind = NSKeyValueChange
let kind: Kind
let newValue: Value?