This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| tell application "System Preferences" | |
| reveal pane "com.apple.preferences.Bluetooth" | |
| end tell | |
| clickBluetoothSwitch() | |
| delay 2 | |
| clickBluetoothSwitch() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Collection where Indices.Iterator.Element == Index { | |
| public subscript(safe index: Index) -> Iterator.Element? { | |
| return (startIndex <= index && index < endIndex) ? self[index] : nil | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Cell | |
| final class CircleCell: UICollectionViewCell { | |
| @IBOutlet private weak var circleView: UIView! | |
| func setCircleView() { | |
| layoutIfNeeded() // Without this, it doesn't draw a circle correctly. | |
| circleView.layer.cornerRadius = circleView.frame.width / 2 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let scale = 10 | |
| let numberStrings = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] | |
| print(numberStrings.prefix(scale)) // ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] | |
| print(numberStrings.suffix(scale - scale)) // [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol StructToDictionary: Encodable { | |
| var dictionary: [String: Any]? { get } | |
| } | |
| extension StructToDictionary { | |
| var dictionary: [String: Any]? { | |
| let encoder = JSONEncoder() | |
| encoder.keyEncodingStrategy = .convertToSnakeCase | |
| guard let data = try? encoder.encode(self) else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UIView { | |
| enum BorderSide { | |
| case left | |
| case right | |
| case top | |
| case bottom | |
| } | |
| func addBorder(toSide side: BorderSide, withColor color: UIColor, withThickness thickness: CGFloat) { | |
| let border = CALayer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Throttler { | |
| private let delay: DispatchTimeInterval | |
| private let queue: DispatchQueue | |
| private var previousFiredTime: DispatchTime = .now() | |
| init(delay: DispatchTimeInterval, queue: DispatchQueue = .main) { | |
| self.delay = delay | |
| self.queue = queue | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Debouncer { | |
| private let delay: DispatchTimeInterval | |
| private let queue: DispatchQueue | |
| private var previousFiredTime: DispatchTime = .now() | |
| init(delay: DispatchTimeInterval, queue: DispatchQueue = .main) { | |
| self.delay = delay | |
| self.queue = queue | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension DispatchQueue { | |
| func debounce(delay: DispatchTimeInterval) -> (_ block: @escaping () -> Void) -> Void { | |
| var lastFireTime: DispatchTime = .now() | |
| return { [weak self, delay] block in | |
| let deadline: DispatchTime = .now() + delay | |
| lastFireTime = .now() | |
| self?.asyncAfter(deadline: deadline) { [delay] in | |
| let now: DispatchTime = .now() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension DispatchQueue { | |
| func throttle(delay: DispatchTimeInterval) -> (_ block: @escaping () -> Void) -> Void { | |
| var lastFireTime: DispatchTime = .now() | |
| return { [weak self, delay] block in | |
| let deadline: DispatchTime = .now() + delay | |
| self?.asyncAfter(deadline: deadline) { [delay] in | |
| let now: DispatchTime = .now() | |
| let when: DispatchTime = lastFireTime + delay |
NewerOlder