Skip to content

Instantly share code, notes, and snippets.

View nh7a's full-sized avatar

Naoki Hiroshima nh7a

View GitHub Profile
public protocol WithConfigurable {}
public extension WithConfigurable where Self: AnyObject {
@discardableResult
func with(block: (Self) -> Void) -> Self {
block(self)
return self
}
}
extension NSObject: WithConfigurable {}
@nh7a
nh7a / XML.swift
Last active November 9, 2020 19:02
A Simple XMLParser wrapper
import Foundation
public struct XML: CustomStringConvertible {
public let root: Element?
public init(string: String) throws {
root = try Parser().parse(data: Data(string.utf8))
}
public var description: String { root?.description ?? "" }
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
}
}
public protocol TypedIdentifier: RawRepresentable {}
extension TypedIdentifier {
public init(_ rawValue: RawValue) {
self.init(rawValue: rawValue)!
}
}
extension TypedIdentifier where RawValue: Encodable {
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)
}
import CoreVideo
enum CVPixelFormatType: RawRepresentable, CaseIterable {
typealias RawValue = OSType
case _1Monochrome
case _2Indexed
case _4Indexed
case _8Indexed
case _1IndexedGray_WhiteIsZero
case _2IndexedGray_WhiteIsZero
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 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! }
import MapKit
struct Polygon {
let coordinates: [CLLocationCoordinate2D]
let mapPoints: [MKMapPoint]
init(coordinates: [CLLocationCoordinate2D]) {
self.coordinates = coordinates
self.mapPoints = coordinates.compactMap(MKMapPointForCoordinate)
}
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