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
| import Foundation | |
| @dynamicMemberLookup | |
| enum JSON: Codable, CustomStringConvertible { | |
| var description: String { | |
| switch self { | |
| case .string(let string): return "\"\(string)\"" | |
| case .number(let double): | |
| if let int = Int(exactly: double) { | |
| return "\(int)" |
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
| import Cocoa | |
| public extension NSApplication { | |
| public func relaunch(afterDelay seconds: TimeInterval = 0.5) -> Never { | |
| let task = Process() | |
| task.launchPath = "/bin/sh" | |
| task.arguments = ["-c", "sleep \(seconds); open \"\(Bundle.main.bundlePath)\""] |
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
| struct Keycode { | |
| // Layout-independent Keys | |
| // eg.These key codes are always the same key on all layouts. | |
| static let returnKey : UInt16 = 0x24 | |
| static let enter : UInt16 = 0x4C | |
| static let tab : UInt16 = 0x30 | |
| static let space : UInt16 = 0x31 | |
| static let delete : UInt16 = 0x33 | |
| static let escape : UInt16 = 0x35 |
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
| public func rubberBand(value: CGFloat, min: CGFloat, max: CGFloat, bandLength: CGFloat) -> CGFloat { | |
| if value >= min && value <= max { | |
| // While we're within range we don't rubber band the value. | |
| return value | |
| } | |
| if bandLength <= 0 { | |
| // The rubber band doesn't exist, return the minimum value so that we stay put. | |
| return min |
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
| /// A statically typed view into a get-only property. | |
| struct TypedGetPropertyView<T> { | |
| /// The name of the property, as a string. | |
| let name : String | |
| /// The actual metatype of the property. | |
| let metatype : T.Type | |
| /// Get the value of the property. | |
| func get() -> T |
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
| /** | |
| # QUESTION | |
| Is there a way to combine `guard` and `try-catch`, so that we can | |
| exploit the `guard let` to have a immutable value, but have an early | |
| return in case of errors? | |
| */ |
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
| // | |
| // ArrayDiff.swift | |
| // | |
| // Created by Frank A. Krueger on 6/30/15. | |
| // Copyright © 2015 Krueger Systems, Inc. All rights reserved. | |
| // License: MIT http://opensource.org/licenses/MIT | |
| // | |
| import Foundation |
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
| // hpp: | |
| /// A smart pointer that can manage the lifecycle of Core Foundation objects. | |
| template<typename T> | |
| class CFPointer { | |
| public: | |
| CFPointer() : storage(nullptr) { } | |
| CFPointer(T pointer) : storage(toStorageType(pointer)) { | |
| if (storage) { |
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
| // #!Swift-1.1 | |
| import Foundation | |
| // MARK: - (1) classes | |
| // Solution 1: | |
| // - Use classes instead of struct | |
| // Issue: Violate the concept of moving model to the value layer | |
| // http://realm.io/news/andy-matuschak-controlling-complexity/ |
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
| NSString *const PSPDFApplicationDidReceiveMemoryWarningNotification = @"PSPDFApplicationDidReceiveMemoryWarningNotification"; | |
| // Test with sudo memory_pressure -l critical. Don't forget to re-set afterwards! | |
| __attribute__((constructor)) static void PSPDFInstallLowMemoryNotificationWarningMac(void) { | |
| static dispatch_source_t source; | |
| static dispatch_once_t onceToken; | |
| dispatch_once(&onceToken, ^{ | |
| source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue()); | |
| dispatch_source_set_event_handler(source, ^{ | |
| dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source); |