I'm storing a set of strings (Set<String>) into a NSUserDefaults.standardUserDefaults() and when retrieving back the object, it comes back as an array of strings instead of a set.
This block works, as I'm recreating the array as a NSSet
if let products = NSUserDefaults.standardUserDefaults().objectForKey("products") as? [String] {
// recreate the array into a NSSet / Set
if let productSet = NSSet(array: products) as? Set<NSObject> {
// now it's a set
}
}
However, it's not possible to get the object directly as a swift Set:
if let products = NSUserDefaults.standardUserDefaults().objectForKey("products") as? Set<String> {
// will not cast into Set<String>
}
I assume NSUserDefaults coverts set into an array internally? Is there a way to receive the items as a set and not an array?
NSUserDefaults.standardUserDefaults().setObject(Set(["a", "b"]), forKey: "products")throws a runtime exception. Without seeing how you set the defaults it is impossible to give a (helpful) answer.