Skip to content

Instantly share code, notes, and snippets.

View RinniSwift's full-sized avatar
🔸
Swifting

Rinni Swift RinniSwift

🔸
Swifting
View GitHub Profile
let callAction = UIAccessibilityCustomAction(
name: "Call",
target: self,
selector: #selector(callButtonPressed)
)
let emailAction = UIAccessibilityCustomAction(
name: "Send email",
target: self,
selector: #selector(emailButtonPressed)
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 }
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)
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] {
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
/// 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
}
struct CachePayload<T: Hashable, U>: Payload {
var key: T
var value: U
init(key: Key, value: Value) {
self.key = key
self.value = value
}
}
protocol Payload {
associatedtype Key
associatedtype Value
var key: Key { get set }
var value: Value { get set }
}
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]
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)