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
| @propertyWrapper | |
| struct Expirable<Value: ExpressibleByNilLiteral> { | |
| let duration: TimeInterval | |
| private var expirationDate = Date() | |
| private var innerValue: Value = nil | |
| private var hasExpired: Bool { | |
| expirationDate < Date() | |
| } |
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 String { | |
| var isBlank: Bool { | |
| return allSatisfy { $0.isWhitespace } | |
| } | |
| } |
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 url = URL(string: "https://youtu.be/k0kSc8hHzAM?t=1461")! | |
| UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (success) in | |
| if !success { | |
| // not a universal link or app not installed | |
| let vc = SFSafariViewController(url: url) | |
| self.present(vc, animated: true) | |
| } | |
| } |
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
| // Combine app + shared defaults into one | |
| extension UserDefaults { | |
| static var shared: UserDefaults { | |
| let combined = UserDefaults.standard | |
| combined.addSuite(named: "com.schnaub.app") | |
| return combined | |
| } | |
| } | |
| // Unit test instance that clears all values before the test is run |
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
| func with<T>(_ x: T, _ f: (inout T) -> Void) -> T { | |
| var value = x | |
| f(&value) | |
| return value | |
| } |
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 Optional where Wrapped: Collection { | |
| var nonEmpty: Wrapped? { | |
| return self?.isEmpty == true ? nil : self | |
| } | |
| } | |
| struct BillingInfo { | |
| var name: String | |
| var vatNumber: String? | |
| } |
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 Predicate<Element> { | |
| private let condition: (Element) -> Bool | |
| func evaluate(for element: Element) -> Bool { | |
| return condition(element) | |
| } | |
| } | |
| func <= <Element, T: Comparable>(_ attribute: KeyPath<Element, T>, _ constant: T) -> Predicate<Element> { | |
| return Predicate(condition: { $0[keyPath: attribute] <= constant }) |
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 Optional where Wrapped: Collection { | |
| var isNilOrEmpty: Bool { | |
| return self?.isEmpty ?? true | |
| } | |
| } | |
| if textField.text.isNilOrEmpty { | |
| // No unwraps | |
| } |
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
| // Float comparison | |
| infix operator ==~ : ComparisonPrecedence | |
| func ==~<T> (lhs: T, rhs: T) -> Bool where T: FloatingPoint { | |
| return lhs == rhs || lhs.nextDown == rhs || lhs.nextUp == rhs | |
| } | |
| // 0 padded numbers | |
| let formatter = NumberFormatter() | |
| formatter.minimumIntegerDigits = 20 | |
| formatter.string(for: 30) // 00000000000000000030 |
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 String { | |
| // [i] | |
| subscript(i: Int) -> Character { | |
| let index = self.index(self.startIndex, offsetBy: i) | |
| return self[index] | |
| } | |
| // [i..<j] | |
| subscript(r: Range<Int>) -> String { | |
| let i = self.index(self.startIndex, offsetBy: r.lowerBound) |