The following code
let cache = NSCache<NSString, [Double]>()
gives me the error:
'NSCache' requires that '[Double]' be a class type
How can I cache an array of Doubles using String
as the key?
As mentioned by others, NSCache is an objC type and will only work with NSObject subclasses. You can use type bridging into NSArray for this.
let cache = NSCache<NSString, NSArray>()
let doubleArray: [Double] = [2.0, 3.0, 4.0]
cache.setObject(doubleArray as NSArray, forKey: "key")
// ...
if let doubleArray = cache.object(forKey: "key") as? [Double] {
// Got my array back
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments