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 | |
| import XCTest | |
| infix operator ^^ : AdditionPrecedence | |
| extension Int { | |
| /// Add `n` to each digit, exclude carry. e.g. | |
| /// ~~~ | |
| /// let shifted = 1945.shiftedUp(by: 2) // 3167 | |
| /// ~~~ |
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
| /// Select two prime numbers `p` and `q` (generally very large numbers, but using small numbers due to limitations of UInt) | |
| let p: UInt = 5 | |
| let q: UInt = 7 | |
| /// `n` is the product of `p` and `q` | |
| let n: UInt = p * q //35 | |
| /// `r` is `(p - 1) * (q - 1)` | |
| let r: UInt = (p-1) * (q-1) //24 |
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 TimeZoneDisplay { | |
| struct TimeZone { | |
| let abbreviation: String | |
| let description: String | |
| let isDaylightSavingsParticipant: Bool | |
| } | |
| var timeZones = [TimeZone]() | |
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
| var versionLabelText: String { | |
| guard let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String, | |
| let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String | |
| else { return "" } | |
| return "App Version: " + version + "." + build | |
| } |
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
| //Doesn't work with all characters - use cautiously. NSAttributedString is preferred. | |
| extension String { | |
| var strikeThrough: String { | |
| guard let strikeThrough = "\u{0336}".unicodeScalars.first else { | |
| return "" | |
| } | |
| return self.map { char -> String in | |
| var c = UnicodeScalarView(char.unicodeScalars) | |
| c.append(strikeThrough) | |
| return String(c) |
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 UIKit | |
| import XCTest | |
| extension String { | |
| var htmlStripped: String? { | |
| data(using: .unicode) | |
| .flatMap { | |
| try? NSAttributedString( | |
| data: $0, | |
| options: [ |
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 UserDefault<Value> { | |
| let key: String | |
| let defaultValue: Value | |
| var wrappedValue: Value { | |
| get { | |
| (UserDefaults.standard.object(forKey: self.key) as? Value) ?? self.defaultValue | |
| } | |
| set { |
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 UIKit | |
| import XCTest | |
| extension Collection { | |
| subscript(guarded idx: Index) -> Element? { | |
| indices.contains(idx) ? self[idx] : nil | |
| } | |
| } | |
| class GuardedSubscriptTests: XCTestCase { |
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 mirrorPrint(_ object: Any, terminator: String = "\n") { | |
| for child in Mirror(reflecting: object).children { | |
| guard let label = child.label else { | |
| Swift.print(child.value, terminator: terminator) | |
| continue | |
| } | |
| Swift.print(label, "=", child.value, terminator: terminator) | |
| } | |
| } |
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 object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? { | |
| guard let data = value(forKey: key) as? Data else { return nil } | |
| return try? decoder.decode(type.self, from: data) | |
| } | |
| func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) { | |
| let data = try? encoder.encode(object) | |
| set(data, forKey: key) | |
| } |