Skip to content

Instantly share code, notes, and snippets.

View nh7a's full-sized avatar

Naoki Hiroshima nh7a

View GitHub Profile
extension UIView {
func autoPinEdgesToSuperviewEdges(with insets: UIEdgeInsets = .zero) {
guard let sv = superview else { return }
translatesAutoresizingMaskIntoConstraints = false
leftAnchor.constraint(equalTo: sv.leftAnchor, constant: insets.left).isActive = true
rightAnchor.constraint(equalTo: sv.rightAnchor, constant: insets.right).isActive = true
topAnchor.constraint(equalTo: sv.topAnchor, constant: insets.top).isActive = true
bottomAnchor.constraint(equalTo: sv.bottomAnchor, constant: insets.bottom).isActive = true
}
}
#import <mach/mach.h>
double cpu_usage() {
thread_array_t threads;
mach_msg_type_number_t thread_count;
if (task_threads(mach_task_self(), &threads, &thread_count) != KERN_SUCCESS) {
return -1;
}
double total = 0;
import Foundation
private let TASK_BASIC_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_basic_info_data_t>.size / MemoryLayout<UInt32>.size)
private let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<UInt32>.size)
enum Process {
static var cpuUsage: Double {
var arr: thread_act_array_t?
var threadCount: mach_msg_type_number_t = 0
import MapKit
struct Polygon {
let coordinates: [CLLocationCoordinate2D]
let mapPoints: [MKMapPoint]
init(coordinates: [CLLocationCoordinate2D]) {
self.coordinates = coordinates
self.mapPoints = coordinates.compactMap(MKMapPointForCoordinate)
}
import XCTest
func XCTAssertEqual2(_ lhs: Any, _ rhs: Any, context: [String] = []) {
func displayStyle(_ value: Any) -> Mirror.DisplayStyle? {
return Mirror(reflecting: value).displayStyle
}
let arrL = Array(Mirror(reflecting: lhs).children).sorted { $0.label! < $1.label! }
let arrR = Array(Mirror(reflecting: rhs).children).sorted { $0.label! < $1.label! }
extension UIResponder {
struct ResponderIterator: IteratorProtocol {
private var responder: UIResponder?
init(_ responder: UIResponder) { self.responder = responder }
mutating func next() -> UIResponder? {
responder = responder?.next
return responder
}
}
import CoreVideo
enum CVPixelFormatType: RawRepresentable, CaseIterable {
typealias RawValue = OSType
case _1Monochrome
case _2Indexed
case _4Indexed
case _8Indexed
case _1IndexedGray_WhiteIsZero
case _2IndexedGray_WhiteIsZero
extension UIDevice {
private static func sysctl(by name: String) -> String {
return name.withCString {
var size = 0
sysctlbyname($0, nil, &size, nil, 0)
var value = [CChar](repeating: 0, count: size)
sysctlbyname($0, &value, &size, nil, 0)
return String(cString: value)
}
public protocol TypedIdentifier: RawRepresentable {}
extension TypedIdentifier {
public init(_ rawValue: RawValue) {
self.init(rawValue: rawValue)!
}
}
extension TypedIdentifier where RawValue: Encodable {
protocol UnknownDecodable: RawRepresentable, Decodable {
static var unknown: Self { get }
}
extension UnknownDecodable where RawValue: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self = Self.init(rawValue: try container.decode(RawValue.self)) ?? .unknown
}
}