| import Foundation | |
| import Combine | |
| enum APIError: Error, LocalizedError { | |
| case unknown, apiError(reason: String) | |
| var errorDescription: String? { | |
| switch self { | |
| case .unknown: | |
| return "Unknown error" |
| @discardableResult | |
| public func with<T>(_ value: T, _ builder: (T) -> Void) -> T { | |
| builder(value) | |
| return value | |
| } | |
| @discardableResult | |
| public func with<T>(_ value: T, _ builder: (T) throws -> Void ) rethrows -> T { | |
| try builder(value) | |
| return value |
| extension UIView { | |
| func findFirstSuperview<T>(ofClass viewClass: T.Type, where predicate: (T) -> Bool) -> T? where T: UIView { | |
| var view: UIView? = self | |
| while view != nil { | |
| if let typedView = view as? T, predicate(typedView) { | |
| break | |
| } | |
| view = view?.superview | |
| } |
| // | |
| // This program is free software. It comes without any warranty, to | |
| // the extent permitted by applicable law. You can redistribute it | |
| // and/or modify it under the terms of the Do What The Fuck You Want | |
| // To Public License, Version 2, as published by Sam Hocevar. See | |
| // | |
| // http://sam.zoy.org/wtfpl/COPYING | |
| // | |
| // for more details. | |
| // |
| import Darwin | |
| @dynamicMemberLookup | |
| struct Environment { | |
| subscript(dynamicMember name: String) -> String? { | |
| get { | |
| guard let value = getenv(name) else { return nil } | |
| return String(validatingUTF8: value) | |
| } |
The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).
My take-aways are:
-
You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.
-
Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse
| // UICollectionView Objective-C example | |
| - (void)viewWillAppear:(BOOL)animated { | |
| [super viewWillAppear:animated]; | |
| NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject]; | |
| if (selectedIndexPath != nil) { | |
| id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator; | |
| if (coordinator != nil) { | |
| [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { |
| import Foundation | |
| extension DateFormatter { | |
| static let iso8601Full: DateFormatter = { | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" | |
| formatter.calendar = Calendar(identifier: .iso8601) | |
| formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| formatter.locale = Locale(identifier: "en_US_POSIX") | |
| return formatter |
| let a: Double? = 1.0 | |
| let b: Double? = 2.0 | |
| let c: Double? = 3.0 | |
| let d: Double? = 4.0 | |
| let e: Double? = 5.0 | |
| let f: Double? = 6.0 | |
| let g: Double? = 7.0 | |
| extension Optional { | |
| func `or`(_ value : Wrapped?) -> Optional { |