Q:

How to cache array of Doubles in Swift

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?

A:

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
}

A:

NSCache is in the Cocoa Objective-C world so you have to play by Cocoa Objective-C rules. You could declare cache as an NSCache<NSString, NSArray>, for example.

