How to use custom Key for NSCache

Issue #766

Need to use a class, best is to subclass from NSObject

1
let cache = NSCache<Key, UIImage>()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
final class Key: NSObject {
override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? Key else {
return false
}
return url == other.url
&& size == other.size
}

override var hash: Int {
return url.hashValue ^ Int(size.width) ^ Int(size.height)
}

let url: URL
let size: CGSize

init(url: URL, size: CGSize) {
self.url = url
self.size = size
}
}

Comments