Skip to content

Instantly share code, notes, and snippets.

View 0xABCCBA's full-sized avatar
🏠
Working from home

3.14≤π 0xABCCBA

🏠
Working from home
View GitHub Profile
@0xABCCBA
0xABCCBA / UIApplication+Extensions.swift
Created March 17, 2023 07:47
UIApplication keyWindow
extension UIApplication {
#if os(iOS)
@available(iOS 13.0, *)
static var customKeyWindow: UIWindow? {
if #available(iOS 15.0, *) {
let window = UIApplication
.shared
.connectedScenes
.compactMap {($0 as? UIWindowScene)?.keyWindow }
@0xABCCBA
0xABCCBA / UIViewController+Extensions.swift
Created March 14, 2023 08:46
Find top visible view controller
extension UIViewController {
func topVisibleViewController() -> UIViewController {
/// A ---- present ---- B ---- present ---- C
/// A is presentingViewController (B.presentingViewController == A)
/// anchor point B <
/// C is presentedViewController (B.presentedViewController == C)
if self.presentedViewController != nil {
return self.presentedViewController!.topVisibleViewController()
@0xABCCBA
0xABCCBA / UIControl+Combine.swift
Last active March 14, 2023 08:49
UIControl+Combine
import Combine
final class EventSubscription<S: Subscriber, C: UIControl>: Subscription where S.Input == C, S.Failure == Never {
private var subscriber: S?
private let control: C
init(subscriber: S, control: C, event: UIControl.Event) {
self.subscriber = subscriber
@0xABCCBA
0xABCCBA / BindingExtension.swift
Created December 16, 2021 03:23
Binding extension of onChange
extension Binding {
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding {
self.wrappedValue
} set: { newValue in
self.wrappedValue = newValue
handler(newValue)
}
}
@0xABCCBA
0xABCCBA / findUniqueNum.swift
Created November 5, 2021 06:00
find unique value in a list containing pairs in linear time
/// findUniqueNum
/// eg: [1, 2, 4, 9, 2, 4, 1] ,find number 9
/// - Parameter arr: [Int]
/// - Returns: resut
func findUniqueNum(in arr: [Int]) -> Int {
return arr.reduce(0) { $0 ^ $1 }
}
@0xABCCBA
0xABCCBA / measureExecuteTime.swift
Created March 18, 2021 05:40
measureExecuteTime
/// 测量一段代码的执行时间
/// - Parameters:
/// - identifier: 标示执行代码的identifier
/// - operation: 执行的代码
func measureExecuteTime(identifier: String, operation: () -> Void) {
let startTime = CFAbsoluteTimeGetCurrent()
operation()
let endTime = CFAbsoluteTimeGetCurrent()
let duration = endTime - startTime
print("\(identifier) execute for \(duration)s.")
/// 以指定的宽度分割数组
/// - Parameters:
/// - arr: 任意数组
/// - each: 每几个一组
/// - Returns: 返回分组后二维数组
func splitArray<T>(arr: [T], by each: Int) -> [[T]] {
var resultArray = [[T]]()
for i in stride(from: 0, to: arr.count, by: each) {
@0xABCCBA
0xABCCBA / Date+MessageString.swift
Last active November 6, 2019 08:27
date or timestamp convert to message string
extension Date {
/// 时间戳转为格式化时间
///
/// - Parameter stamp: 时间戳
/// - Returns: 格式化时间
static func toMessageString(stamp: TimeInterval) -> String {
let date = Date(timeIntervalSince1970: stamp)
return toMessageString(date)
@0xABCCBA
0xABCCBA / UIDevice+XMUtils.h
Created October 15, 2019 11:31 — forked from kangzubin/UIDevice+XMUtils.h
The sample code to get iPhone device name.
//
// UIDevice+XMUtils.h
// AwesomeTips
//
// Created by kangzubin on 2018/9/20.
// Copyright © 2018 KANGZUBIN. All rights reserved.
//
#import <UIKit/UIKit.h>
@0xABCCBA
0xABCCBA / generatePictureForAView.swift
Created January 21, 2019 07:25
generatePictureForAView
/// 为UIView的内容生成一张图片
///
/// - Parameter view: 生成图片的View
/// - Returns: 返回生成的图片
func generatePictureIn(view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
guard let ctx = UIGraphicsGetCurrentContext() else { return nil }
view.layer.render(in: ctx)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()