How to cache array of Doubles in Swift

AJ Z.

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?

Fernando Mazzon

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert pyramid type of String to array of doubles in Swift 4

Initializing an array of Doubles in Swift 4

How to create a list of doubles out of an array of doubles

Swift - Convert values in array to doubles or floats

How to convert array of doubles into JS array?

How to convert array of floats to array of doubles in Java?

How to convert an array of array of Doubles to an RDD[String]

How to convert a dataframe of array of doubles to Vectors?

How to multiply array of doubles in another double?

how to cast doubles from char array

How do I generate random doubles in an Array?

How to iterate through an object that is pointing to array of doubles?

Swift iOS Map an Array Of Doubles, Convert them to Date, then Sort

How do I cast an array of doubles into an array of integers in Java?

How do I display multiple doubles and strings in swift?

How to clear cache on UIWebView in swift?

Understanding the Doubles and Ints in Swift

swift nil coalescing with doubles

NSIntegers for Doubles in Swift

Are Doubles Comparable in Swift

Algorithm for LCM of doubles in Swift

Multiplying variables and doubles in swift

Is there a placeholder for doubles in Swift?

Cache Array of Custom Objects in Swift3

Swift Ints and some Doubles working but not all Doubles

How can I make a for-loop with array of doubles?

how to define a comparison operator (less than) on array of doubles?

Java How to iterate through an array of doubles and round to nearest whole number

How to get a float array from a List of 3 doubles?

TOP Ranking

HotTag

Archive