This file contains 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 Notification<T> { | |
let name: NSNotification.Name | |
} | |
private let notificationData = "_notificationData" | |
extension NotificationCenter { | |
func post<T>(_ notification: Notification<T>, object: Any? = nil, data: T) { | |
post(name: notification.name, object: object, userInfo: [notificationData: data]) | |
} |
This file contains 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 Validator where Input == User, Output == User { | |
static var validUser: Validator<User, User> { | |
return .keyPath(\.name, .isNotNil && .isNotEmpty) | |
} | |
} | |
struct User { | |
let name: String? | |
} |
This file contains 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 | |
/// An abstract class that makes building simple asynchronous operations easy. | |
/// Subclasses must override `main()` to perform any work and call `finish()` | |
/// when they are done. All `NSOperation` work will be handled automatically. | |
/// | |
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233 | |
open class AsyncOperation: Operation { | |
public init(name: String? = nil) { | |
super.init() |
This file contains 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 UIKit | |
/// A validation rule for text input. | |
public enum TextValidationRule { | |
/// Any input is valid, including an empty string. | |
case noRestriction | |
/// The input must not be empty. | |
case nonEmpty | |
/// The enitre input must match a regular expression. A matching substring is not enough. | |
case regularExpression(NSRegularExpression) |
This file contains 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 | |
extension Decodable { | |
public static func randomInstance() throws -> Self { | |
let decoder = RandomDecoder() | |
return try Self(from: decoder) | |
} | |
} | |
private class RandomDecoder: Decoder { |
This file contains 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 | |
// MARK: SortDescriptor | |
/// Type-erased Sort Descriptor (can store multiple in the same array | |
/// regardless of the underlying KeyPath | |
public struct SortDescriptor<Element> { | |
private let comparator: (Any, Any) -> Bool |
This file contains 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 extension Dictionary { | |
public func mapPairs<T: Hashable, U>(_ transform: ((key: Key, value: Value)) throws -> (key: T, value: U)) rethrows -> [T: U] { | |
return .init(uniqueKeysWithValues: try self.map(transform)) | |
} | |
} | |
let x = ["1": 1, "2": 2, "3": 3] | |
let y = x.mapPairs { (Int($0.key)!, "\($0.value)") } | |
print(x) // ["1": 1, "2": 2, "3": 3] |
This file contains 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 UIKit | |
class PassThroughView: UIView { | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
let result = super.hitTest(point, with: event) | |
if result == self { return nil } | |
return result | |
} | |
This file contains 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 wrapper for a partial representation of a `T`. | |
/// It can be constructed over time, then later used to | |
/// build a complete `T` | |
@dynamicMemberLookup | |
public struct Partial<T> { | |
enum Error: Swift.Error { | |
case invalid(PartialKeyPath<T>) | |
} | |
// MARK: - Private Properties |
This file contains 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 | |
extension Data { | |
var hexString: String { | |
return self.map({ return String(format: "%02hhx", $0) }).joined() | |
} | |
} |