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 enum NilOrdering { | |
| case first, last | |
| } | |
| public enum SortOrdering { | |
| case ascending, descending | |
| } | |
| public extension Sequence { | |
| func sorted(on accessor: (Element) -> some Comparable, ordering: SortOrdering = .ascending) -> [Element] { |
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
| protocol CaseCountable { } | |
| extension CaseCountable where Self : RawRepresentable, Self.RawValue == Int { | |
| static var count: Int { | |
| var count = 0 | |
| while let _ = Self(rawValue: count) { count+=1 } | |
| return count | |
| } |
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
| //stolen from @publicextension | |
| extension Sequence { | |
| func groupBy<T>(groupBy: (Iterator.Element) -> T) -> [T: [Iterator.Element]] { | |
| var result: [T: [Iterator.Element]] = [:] | |
| self.forEach { element in | |
| let groupKey = groupBy(element) | |
| result[groupKey] = (result[groupKey] ?? []) + [element] | |
| } |
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 SignedInteger { | |
| func times(_ each: () -> ()) { | |
| var count: Self = 0 | |
| while (count < self) { each(); count = count + 1 } | |
| } | |
| } | |
| extension SignedInteger { |
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 Security | |
| private let DataKey = "MyDataKey" | |
| /** | |
| * The KeychainResult class wraps an OSStatus that is returned from a keychain action. It vends KeychainErrors. | |
| */ | |
| struct KeychainResult { | |
| let keychainError: OSStatus |
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 HeaderFormatter { | |
| var text: String | |
| var attributedString: NSAttributedString? { | |
| guard let font = UIFont.champion(ofSize: 32) else { return nil } | |
| let attributes: [NSString : AnyObject] = [NSFontAttributeName as NSString: font, NSForegroundColorAttributeName as NSString: UIColor.black, NSKernAttributeName as NSString : 1.5 as AnyObject] | |
| return NSAttributedString(string: text, attributes: attributes as [String : Any]?) | |
| } |
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 | |
| extension Array where Element: Comparable { | |
| mutating func quickSort() { | |
| self[self.indices].quickSort() | |
| } | |
| } | |
| extension ArraySlice where Element: Comparable { | |
| mutating func quickSort() { |
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 | |
| let a = ["Asdf", "asdf"] | |
| let nsArray1: NSMutableArray = a.reduce(NSMutableArray(), { array, task in array.add(task); return array }) | |
| nsArray1 | |
| let nsArray2 = NSArray(objects: a) | |
| let nsArray3 = NSArray(array: a) | |
| let nsArray4 = a as NSArray |
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 BinaryFloatingPoint { | |
| func rounded(to radix: Self) -> Self { | |
| return (self/radix).rounded(.toNearestOrAwayFromZero)*radix | |
| } | |
| } | |
| (55.325).rounded(to: 1/10) // => 55.3 |
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 | |
| final class Atomic<T> { | |
| private let queue = DispatchQueue(label: "com.atomic.queue", attributes: [.concurrent]) | |
| private var value: T { | |
| get { | |
| return queue.sync { | |
| return self.value |