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 callAction = UIAccessibilityCustomAction( | |
| name: "Call", | |
| target: self, | |
| selector: #selector(callButtonPressed) | |
| ) | |
| let emailAction = UIAccessibilityCustomAction( | |
| name: "Send email", | |
| target: self, | |
| selector: #selector(emailButtonPressed) |
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 element = UIAccessibilityElement(accessibilityContainer: self) | |
| element.accessibilityLabel = [titleLabel.text, subtitleLabel.text].compactmap { $0 }.joined(seperator: ", ") | |
| element.accessibilityFrameInContainerSpace = titleLabel.frame.union(subtitleLabel.frame) | |
| accessibilityElements = [element, bottomView, button].compactMap { $0 } |
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
| class LRUCache<T: Hashable, U> { | |
| // ... | |
| /// Returns the element at the specified key. Nil if it doesn't exist. | |
| func retrieveObject(at key: T) -> U? { | |
| guard let existingNode = dictionary[key] else { | |
| return nil | |
| } | |
| linkedList.moveToHead(node: existingNode) |
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
| class LRUCache<T: Hashable, U> { | |
| // ... | |
| /// Sets the specified value at the specified key in the cache. | |
| func setObject(for key: T, value: U) { | |
| let element = CachePayload(key: key, value: value) | |
| let node = Node(value: element) | |
| if let existingNode = dictionary[key] { |
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
| class LRUCache<T: Hashable, U> { | |
| /// Total capacity of the LRU cache. | |
| private(set) var capacity: UInt | |
| /// LinkedList will store elements that are most accessed at the head and least accessed at the tail. | |
| private(set) var linkedList = DoublyLinkedList<CachePayload<T, U>>() | |
| /// Dictionary that will store the element, U, at the specified key. | |
| private(set) var dictionary = [T: Node<CachePayload<T, U>>]() | |
| /// LRUCache requires a capacity which must be greater than 0 |
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
| /// A Node class to represent data objects in the LinkedList class | |
| class Node<T: Payload> { | |
| var payload: T | |
| var previous: Node<T>? | |
| var next: Node<T>? | |
| init(value: T) { | |
| self.payload = 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
| struct CachePayload<T: Hashable, U>: Payload { | |
| var key: T | |
| var value: U | |
| init(key: Key, value: Value) { | |
| self.key = key | |
| self.value = 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
| protocol Payload { | |
| associatedtype Key | |
| associatedtype Value | |
| var key: Key { get set } | |
| var value: Value { get 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
| var dict = ["blue": 2, "grey": 1, "red": 2] | |
| var newDict = ["pink": 3, "red": 5] | |
| dict.merge(newDict) { (current, _) in current } // ["blue": 2, "grey": 1, "red": 2, "pink": 3] | |
| dict.merge(newDict) { (_, new) in new } // ["blue": 2, "grey": 1, "red": 5, "pink": 3] | |
| let mergedDict = dict.merging(newDict) { (current, _) in current} // ["blue": 2, "grey": 1, "red": 5, "pink": 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
| var dict = ["blue": 2, "grey": 1, "red": 2] | |
| // for-each loop | |
| dict.forEach { pair in | |
| print(pair) | |
| } | |
| // for-in loop | |
| for pair in dict { | |
| print(pair) |
NewerOlder