Skip to content

Instantly share code, notes, and snippets.

View ha1f's full-sized avatar

はるふ ha1f

View GitHub Profile
private extension WKWebView {
func getUserAgent() async throws -> String? {
try await withCheckedThrowingContinuation { continuation in
evaluateJavaScript("navigator.userAgent") { (result, error) in
if let error {
continuation.resume(throwing: error)
} else if let userAgent = result as? String {
continuation.resume(returning: userAgent)
}
}
extension UIView {
var recursiveSubviews: [UIView] {
subviews + subviews.flatMap(\.recursiveSubviews)
}
func printViewHierarchy(depth: Int = 0) {
for subview in subviews {
print("\(String(repeating: " ", count: depth))- \(String(describing: type(of: subview)))")
subview.printViewHierarchy(depth: depth + 1)
}
/// 指定した桁数で2進数を表示する、0埋め
func formatBinary8(_ num: UInt8, digits: Int = 8) -> String {
let s = String(num, radix: 2)
return s.count < digits
? String(repeating: "0", count: digits - s.count) + s
: s
}
/// パスから末尾の拡張子を取得する
function getFileExtension(path) {
let splitted= url.split(".");
return splitted[splitted.length - 1];
}
/// Google Driveのルート直下の指定の名前の
/// フォルダを作成または取得する
function getTargetFolder(folderName) {
let rootFolder = DriveApp.getRootFolder();
import Foundation
final class WeakObject<T: AnyObject> {
weak var _object: T?
init(_ object: T) {
_object = object
}
}
private func _hexString<I: BinaryInteger>(_ value: I) -> String {
"0x" + String(value, radix: 16)
}
extension Character {
func hexRepresentation() -> String {
unicodeScalars
.map { _hexString($0.value) }
.joined(separator: ", ")
}
extension AVAsset {
private func _generator(size: CGSize) -> AVAssetImageGenerator {
let generator = AVAssetImageGenerator(asset: self)
generator.maximumSize = size
generator.appliesPreferredTrackTransform = true
return generator
}
func extractUIImageAsync(size: CGSize) -> Single<UIImage?> {
let generator = _generator(size: size)
public extension KeyedDecodingContainerProtocol {
func decode<T: Decodable>(forKey key: Key) throws -> T {
try decode(T.self, forKey: key)
}
func decodeSafe<T: Decodable>(forKey key: Key, defaultValue: T) -> T {
(try? decode(T.self, forKey: key)) ?? defaultValue
}
}
import Foundation
public protocol EmptyInitializable {
init()
}
extension Int: EmptyInitializable {}
extension String: EmptyInitializable {}
extension Array: EmptyInitializable {}
extension Bool: EmptyInitializable {}
//
// UIKit+lerp.swift
// ha1f
//
extension CGRect {
/// 線形補間した中間矩形を返す
/// - parameter otherRect: 目指す先の矩形
/// - parameter rate: 0-1の間の値。0なら自分と、1ならotherRectと同じ。
func lerp(_ otherRect: CGRect, rate: CGFloat) -> CGRect {