Skip to content

Instantly share code, notes, and snippets.

View nh7a's full-sized avatar

Naoki Hiroshima nh7a

View GitHub Profile
#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;
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
}
}
final class AtomicCounter {
private let lock = DispatchSemaphore(value: 1)
private var value: Int
init(_ value: Int = 0) {
self.value = value
}
static func +=(atomic: inout AtomicCounter, value: Int) {
atomic.lock.wait(); defer { atomic.lock.signal() }
extension ExpressibleByIntegerLiteral {
static var random: Self {
var r: Self = 0
arc4random_buf(&r, MemoryLayout<Self>.size)
return r
}
}
Int8.random
UInt8.random
import Foundation
protocol CamelSnakeConvertible {
func snake_cased() -> String
func camelCased() -> String
}
extension CamelSnakeConvertible {
func camelCased() -> String {
let arr = snake_cased().components(separatedBy: "_")
class Queue<T> {
private var array: [T]
private var dispatch: DispatchQueue
let name: String
init(label: String? = nil) {
array = []
name = label ?? UUID().uuidString
dispatch = DispatchQueue(label: name)
// This strictly typed notification idea is not my invention but @naan's.
// This simple implementation of the idea may not work suitably for an environment where
// you need to be able to selectively observe events by the sender, but works great in a
// simpler environment such as the most of iOS apps.
import Foundation
protocol TypedNotification {
static var name: Notification.Name { get }
init(notification: Notification)
import Foundation
protocol EnumAllCases: Hashable {}
extension EnumAllCases {
static var allCases: [Self] {
var raw = 0
return Array<Self>(
AnyIterator {
defer { raw += 1 }
return withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: Self.self, capacity: 1) { $0.pointee } }
extension UITabBarController {
func wiggle(index: Int, duration: CFTimeInterval = 0.4) {
guard index + 1 < tabBar.subviews.count else { return } // Depending on the hidden structure
guard let v = tabBar.subviews[index + 1].subviews.first as? UIImageView else { return }
let a: CAKeyframeAnimation = {
func nsnumber(n: Double) -> NSNumber { return NSNumber(value: n) }
let values = [0, 0.2, -0.2, 0.2, -0.2, 0.1, 0]
let keyTimes = [0.0, 2.0/12.0, 3.0/12.0, 6.0/12.0, 9.0/12.0, 10.0/12, 1.0]
import XCTest
@testable import foobar
class foobarTests: XCTestCase {
private func loadInputFile(_ name: String) -> String {
let arr = #file.components(separatedBy: "/")
let path = (arr.prefix(upTo: arr.count-1) + [name]).joined(separator: "/")
do {
return try String(contentsOfFile: path)
} catch {